miskit74
miskit74

Reputation: 3

keep getting a NullPointerException error, can't find where

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?

Upvotes: 0

Views: 245

Answers (3)

user370305
user370305

Reputation: 109237

Just check the your TextView shots.getEditableText().toString() value, Either is not empty or it contains digits not other alphabets.

And it something like,

 shotsDouble = Double.valueOf(shots.getText().toString().trim());  

One more thing,

public Editable getEditableText ()

Return the text the TextView is displaying as an Editable object. If the text is not editable, null is returned.

Upvotes: 1

Shashank Kadne
Shashank Kadne

Reputation: 8101

It seems like getEditableText() is returning null.

The documentation says..

public Editable getEditableText ()

Return the text the TextView is displaying as an Editable object. If the text is not editable, null is returned..

So just try replacing it with getText().

Upvotes: 0

Alex Lockwood
Alex Lockwood

Reputation: 83303

I suggest you use the Log class to debug your application. Add calls to

Log.v("Activity name", "line number: #");

at various points in your Java file, run your application, and check the logcat to see which was the last to get called before the NullPointerException was thrown.

Upvotes: 0

Related Questions