Null Pointer Exception error on button click - java

Line 33, all debug logs are fine.
As u can see I even comented the method that is causing problems and it still crashesh.
MainActivity.java
package com.example.hillsmatrixinverser;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Math matrix;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText Text1 = (EditText) findViewById(R.id.a) ;
final EditText Text2 = (EditText) findViewById(R.id.b) ;
final EditText Text3 = (EditText) findViewById(R.id.c) ;
final EditText Text4 = (EditText) findViewById(R.id.d) ;
// final EditText TextFinal = (EditText) findViewById(R.id.editText1) ;
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//
Log.d("TEXT1", Text1.getText().toString());
Log.d("TEXT2", Text2.getText().toString());
Log.d("TEXT3", Text3.getText().toString());
Log.d("TEXT4", Text4.getText().toString());
matrix.GetValues(Text1.getText().toString(), Text2.getText().toString(),
Text3.getText().toString(), Text4.getText().toString());
// TextFinal.setText(matrix.Calculate());
}
});
}
}
class:
package com.example.hillsmatrixinverser;
import android.app.Application;
public class Math {
public int a;
public int b;
public int c;
public int d;
public void GetValues(String a1, String b1, String c1, String d1){
// Integer.parseInt(a1);
// Integer.parseInt(b1);
// Integer.parseInt(c1);
// Integer.parseInt(d1);
}
public String Calculate(){
return Integer.toString(a+b+c+d);
}
}
Line causing problems:
matrix.GetValues(Text1.getText().toString(), Text2.getText().toString(),
Text3.getText().toString(), Text4.getText().toString());

You never initialize Math matrix;
So when you're trying to call your GetValues method on your matrix object, it throws the NPE.

Related

Button's onClick property does not show my action

I was trying to program a simple calculator in Android Studio, only with plus, minus, times and divide functions. I am half-code in programming of "plus" function, which is triggered by clicking a button. Here is my code in MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class Arithmetic {
public void plus(View view) {
EditText editText = (EditText) findViewById(R.id.editText);
EditText editText2 = (EditText) findViewById(R.id.editText2);
TextView textView = (TextView) findViewById(R.id.textView);
String numberOne = editText.getText().toString();
String numberTwo = editText2.getText().toString();
String result = numberOne + numberTwo;
}
}}
Now I have two questions. Firstly, how do I make it to show result in textView? Secondly, public void plus is not showing in button's box onClick. Why? And how do I make it to show there?
I will be really thankful for every useful tip.
1.Firstly, how do I make it to show result in textView
int sum = (Integer.parseInt(numberOne)) + (Integer.parseInt(numberTwo));
String result = sum + "";
textView.setText(result);
//or
textView.setText(sum + "");
2.Why public void plus is not showing in button's box onClick
<button
----
android:onClick = "plus"/>
and
public class MainActivity extends AppCompatActivity{
onCreate().....
public void plus(View view){
//your button code here
}
No need to create a nested class for that!
This is what you are looking for, but you should learn some Android and Java basics before starting a project:
Remove the onClick property of your button in your xml file, it's better to use a OnClickListener (particularly if you are planning to use multiple buttons)
package com.example.arnaudpradier.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button calculBtn;
EditText inputFieldOne;
EditText inputFieldTwo;
TextView showResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calculBtn = (Button) findViewById(R.id.calculateBtn);
inputFieldOne = (EditText) findViewById(R.id.inputFieldOne);
inputFieldTwo = (EditText) findViewById(R.id.inputFieldTwo);
showResult = (TextView) findViewById(R.id.resultText);
calculBtn.setOnClickListener(this);
}
private void makeAddition() {
int firstValue = (Integer.parseInt(inputFieldOne.getText().toString()));
int secondValue = (Integer.parseInt(inputFieldTwo.getText().toString()));
String result = (firstValue + secondValue) + "";
showResult.setText(result);
}
#Override
public void onClick(View view) {
int i = view.getId();
if (i == R.id.calculateBtn) {
makeAddition();
}
}
}
public class MainActivity extends AppCompatActivity {
EditText editText;
EditText editText2;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
textView = (TextView) findViewById(R.id.textView);
Arithmetic a = new Arithmetic();
Arithmetic.plus();
}
public class Arithmetic {
public void plus() {
String numberOne = editText.getText().toString();
String numberTwo = editText2.getText().toString();
String result = numberOne + numberTwo;
textView.setText(result);
}
}}

How to use radio buttons for math operation in android

[Android newbie] Help required in adding 4 math operations as options to a radion button.
Also I need to perform respective options on selecting radio button option.
package com.sivaneshsg.wallet;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
int cashamount = 0;
int cardamount = 0;
int walletamount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et = (EditText) findViewById(R.id.inputamount);
final RadioButton card1 = (RadioButton) findViewById(R.id.card1);
final RadioButton cash1 = (RadioButton) findViewById(R.id.cash1);
final RadioButton card2 = (RadioButton) findViewById(R.id.card2);
final RadioButton cash2 = (RadioButton) findViewById(R.id.cash2);
final RadioGroup rg =(RadioGroup) findViewById(R.id.rgroup);
final TextView t1 = (TextView) findViewById(R.id.amountcard);
final TextView t2 = (TextView) findViewById(R.id.amountcash);
final TextView t3 = (TextView) findViewById(R.id.amountwallet);
Button but = (Button) findViewById(R.id.button);
final int amount = Integer.parseInt(et.getText().toString());
cash1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cash2.setChecked(false);
card1.setChecked(false);
card2.setChecked(false);
cashamount = cashamount + amount;
}
});
card1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cash2.setChecked(false);
cash1.setChecked(false);
card2.setChecked(false);
cardamount=cardamount+amount;
}
});
cash2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cash1.setChecked(false);
card1.setChecked(false);
card2.setChecked(false);
cashamount = cashamount - amount;
}
});
card2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cash2.setChecked(false);
cash1.setChecked(false);
card2.setChecked(false);
cardamount=cardamount-amount;
}
});
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
t1.setText("Amount in Card : RS. " + cardamount);
t2.setText("Amount in Cash : Rs. " + cashamount);
walletamount = cardamount + cashamount;
t3.setText("Total Amount in Wallet : RS. " + walletamount);
}
});
}
}
your amount variable taking value at onCreate() method which is initially 0

Trying to multiply a TextField value by a double value

I am trying to get the value of inputEditText and multiply it by the value of convertNum.
The error I'm getting is: Operator '*' cannot be applied to 'android.widget.EditText','double'
I believe I need to convert the value of inputEditText into a number, but I'm not sure. Any help would be appreciated. Here is my code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare variables
final Button calculateButton = (Button) findViewById(R.id.button);
final TextView resultTextView = (TextView) findViewById(R.id.textView2);
final TextView resultnumTextView = (TextView) findViewById(R.id.textView3);
final EditText inputEditText = (EditText) findViewById(R.id.editText);
final double convertNum = 0.125;
// Set resultTextView to be invisible on app start
resultTextView.setVisibility(View.INVISIBLE);
// When calculate button is pressed: Set resultTextView to visible and show result
calculateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
resultTextView.setVisibility(View.VISIBLE);
resultnumTextView.setText(inputEditText * convertNum);
}
});
}
You get the error as you are trying to multiply EditText (which is a View instance) and double (which is a number).
Easiest fix is to replace
resultnumTextView.setText(inputEditText * convertNum);
with
resultnumTextView.setText(String.valueOf(
Float.valueOf(inputEditText.getText().toString()) * convertNum
));
Instead of this:
resultnumTextView.setText(inputEditText * convertNum);
Do this:
resultnumTextView.setText(Double.parseDouble(inputEditText.getText().toString()) * convertNum);
inputEditText is an EditText, a widget to write text. You need to extract the typed text and convert into a float.
inputEditText.getText().toString()
Will give you a String with the typed text. Then use the wrapper class Float to convert into a number
String s = inputEditText.getText().toString();
Float f= Float.parseFloat(s);
As you can see here
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare variables
final Button calculateButton = (Button) findViewById(R.id.button);
final TextView resultTextView = (TextView) findViewById(R.id.textView2);
final TextView resultnumTextView = (TextView) findViewById(R.id.textView3);
final EditText inputEditText = (EditText) findViewById(R.id.editText);
final double convertNum = 0.125;
// Set resultTextView to be invisible on app start
resultTextView.setVisibility(View.INVISIBLE);
// When calculate button is pressed: Set resultTextView to visible and show result
calculateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String valueFromInput = inputEditText.getText().toString();
try{
double valueToDouble = Double.parseDouble(valueFromInput);
double result = convertNum * valueToDouble;
resultnumTextView.setText(result+"");
resultTextView.setVisibility(View.VISIBLE);
}catch(Exception e){
// print the exception.
}
}
});
}

app keeps crashing

hey I'm new to programming so I thought I would just make a simple c to f converter but it keeps crashing on the emulator before I open it can anyone see why? i know the codes probally all wrong but its the only way i could think of doing it
package com.jamie.convert;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ConvertorActivity extends Activity implements OnClickListener {
TextView textout1;
EditText textIn1;
Button convert;
int x=Integer.parseInt(textIn1.getText().toString());
double fahrenheit = 1.8*x;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
convert = (Button) findViewById(R.id.convert);
textIn1=(EditText) findViewById(R.id.input);
textout1=(TextView) findViewById(R.id.output);
convert.setOnClickListener((android.view.View.OnClickListener)this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.convert) {
textout1.setText(""+fahrenheit);
}
}
}
use this code instead of this you'll get the answer
TextView textout1;
EditText textIn1;
Button convert;
static int x;
//int x=Integer.parseInt(textIn1.getText().toString());
//double fahrenheit = 1.8*x;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
convert = (Button) findViewById(R.id.button1);
textIn1=(EditText) findViewById(R.id.editText1);
textout1=(TextView) findViewById(R.id.textView1);
convert.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
x=Integer.parseInt(textIn1.getText().toString());
double fahrenheit = 1.8*x;
textout1.setText(""+fahrenheit);
}
});
}
This would heppened due to your int x would not be getting any value from the edittext.
int x=Integer.parseInt(textIn1.getText().toString());
The above line will be executed immediately the Activity is started. At this point, textIn1 will be null.
Also this line shouldn't be there in this way...
double fahrenheit = 1.8*x;
Replace those lines with...
int x;
double fahrenheit;
...and then change the onClick() method to...
#Override
public void onClick(View v) {
if(v.getId() == R.id.convert) {
x = Integer.parseInt(textIn1.getText().toString());
fahrenheit = x * 1.8 + 32
textout1.setText(String.valueOf(fahrenheit));
}
}
try to String.valueOf(fahrenheit);
instead of fahrenheit in your onClick
also move your
int x=Integer.parseInt(textIn1.getText().toString());
to the onclick methode

Attempted GCF app for Android

I am new to Android and am trying to create a very basic app that calculates and displays the GCF of two numbers entered by the user. Here is a copy of my GCF.java:
package com.example.GCF;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class GCF extends Activity {
private TextView mAnswer;
private EditText mA, mB;
private Button ok;
private String A, B;
private int iA, iB;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mA = (EditText) findViewById(R.id.entry);
mB = (EditText) findViewById(R.id.entry1);
ok = (Button) findViewById(R.id.ok);
mAnswer = (TextView) findViewById(R.id.answer1);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
A = mA.getText().toString();
B = mB.getText().toString();
}
});
// the String to int conversion happens here
iA = Integer.parseInt(A.trim());
iB = Integer.parseInt(B.trim());
while (iA != iB) {
int[] nums={ iA, iB, Math.abs(iA-iB) };
Arrays.sort(nums);
iA=nums[0];
iB=nums[1];
}
updateDisplay();
}
private void updateDisplay() {
mAnswer.setText(
new StringBuilder().append(iA));
}
}
Any Suggestions? Thank you!
You probably want this ( for your onCreate function )
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mA = (EditText) findViewById(R.id.entry);
mB = (EditText) findViewById(R.id.entry1);
ok = (Button) findViewById(R.id.ok);
mAnswer = (TextView) findViewById(R.id.answer1);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
A = mA.getText().toString();
B = mB.getText().toString();
// the String to int conversion happens here
iA = Integer.parseInt(A.trim());
iB = Integer.parseInt(B.trim());
while (iA != iB) {
int[] nums={ iA, iB, Math.abs(iA-iB) };
Arrays.sort(nums);
iA=nums[0];
iB=nums[1];
}
updateDisplay();
}
});
}

Categories

Resources