I am sure this is really a simple solution that I am just not seeing. However, I changed some features (which seems unrelated), but now I am getting a null pointer exception error and it just isn’t making itself obvious to where I can find it. If anyone could look at my code and help me out. I have been able to narrow it down to a key section of my code.
This code triggers an alert dialog:
tableRowShots = (TableRow)findViewById(R.id.shots_row);
tableRowShots.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
shotTextDialog().show();
calculate();
}
});
here is the code for the shotTextDialog():
private Dialog shotTextDialog() {
final View layout = View.inflate(this, R.layout.amount_edittext, null);
final EditText savedText = (EditText) layout.findViewById(R.id.amountEditText);
textViewShotsAmount = (TextView)findViewById(R.id.shots_amount);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose how many shots:");
builder.setPositiveButton("Save", new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myTextString = savedText.getText().toString().trim();
TextView sizeText = (TextView)findViewById(R.id.shots_amount);
sizeText.setText(myTextString);
Toast.makeText(getApplicationContext(), "You have chosen " + myTextString + " shots for your drink", Toast.LENGTH_SHORT).show();
}
});
builder.setView(layout);
return builder.create();
}
here is the code for calculate():
public void calculate() {
double leftoverLiquid = 0;
TextView shots = (TextView) this.findViewById(R.id.shots_amount);
double shotsDouble = 0;
if(shots != null) {
shotsDouble = Double.valueOf(shots.getEditableText().toString());
}
TextView powder = (TextView) this.findViewById(R.id.powder_amount);
double powderDouble = 0;
if(powder != null) {
powderDouble = Double.valueOf(powder.getText().toString());
}
TextView add1 = (TextView) this.findViewById(R.id.additive_amount_1);
double add1Double = 0;
if(add1 != null) {
add1Double = Double.valueOf(add1.getEditableText().toString());
}
TextView add2 = (TextView) this.findViewById(R.id.additive_amount_2);
double add2Double = 0;
if(add2 != null) {
add2Double = Double.valueOf(add2.getEditableText().toString());
}
leftoverLiquid = size() - (powderDouble + shotsDouble + add1Double + add2Double);
TextView liquid = (TextView) findViewById(R.id.liquid_amount);
textViewLiquidAmount = liquid;
liquid.setText(Double.toString(leftoverLiquid));
}
The NullPointerException error seems to fall somewhere near the line of code:
shotsDouble = Double.valueOf(shots.getEditableText().toString());
Can anyone see anything that I am missing?
Just check the your TextView
shots.getEditableText().toString()value, Either is not empty or it contains digits not other alphabets.And it something like,
One more thing,
Return the text the TextView is displaying as an Editable object. If the text is not editable, null is returned.