user3124306 user3124306 - 8 months ago 26
Android Question

Null pointer exception with values on text change listener android

I have code to simply calculate compound interest. The calculation will change when the user edits the value in an EditText view.

public class CompoundInterest extends AppCompatActivity {

TextView mCompBalance;
TextView mBalance;
EditText mYears;
Integer balance;
Integer years;
Double compbalance;
Double interest;
boolean isNumber;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compound_interest);

interest = 0.025;

mBalance = (TextView) findViewById(R.id.tv_savingsbalance);
mCompBalance = (TextView) findViewById(R.id.tv_interestbalance);
mYears = (EditText) findViewById(R.id.et_years);
System.out.println(mYears.getText().toString());

Resources res = getResources();
balance = Integer.valueOf(res.getString(R.string.savings_balance));
mBalance.setText("£"+ balance);


mYears.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (mYears.getText().toString().matches("\\d+(?:\\.\\d+)?")){
years = Integer.valueOf(mYears.getText().toString());
System.out.println(years);
isNumber =true;
} else {
isNumber =false;
}

if (isNumber){
compbalance = balance*Math.pow(1+interest, years);
mCompBalance.setText("£"+String.format("%.2f", compbalance));
}

}

@Override
public void afterTextChanged(Editable editable) {

}
});


}
}


When I try to edit the EditText view that sets the number of years for the calculation - I get this error and the app crashes:


java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
at com.###.android.citiapp.CompoundInterest$1.onTextChanged(CompoundInterest.java:63)
at android.widget.TextView.sendOnTextChanged(TextView.java:8187)
at android.widget.TextView.handleTextChanged(TextView.java:8249)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10371)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1208)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:578)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:230)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:229)
at android.view.inputmethod.BaseInputConnection.deleteSurroundingText(BaseInputConnection.java:251)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:459)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:93)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)


I'm not sure what's going on.

Answer Source

Use charSequence.toString() instead of mYears.getText().toString() in onTextChanged function. Hope it helps or specify exactly where NullPointerException is occurring?