简体   繁体   中英

My Simple Interest calculator is crashing with a null pointer exception error

My Simple Interest calculator is crashing with a null pointer exception error, not sure what the problem is, there are no errors in the IDE before I complile, this is new to me. Here is my code and logcat: edit: Icouldn't post a logcat as the editor thinks it's code and I can't get it to format correctly Code:

package com.codeherenow.sicalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.SeekBar;

public class SICalculatorActivity extends Activity
    implements SeekBar.OnSeekBarChangeListener, View.OnClickListener{
private int years;
private TextView YT;
private SeekBar bar;
private EditText principal = (EditText) findViewById(R.id.PA_field);
private EditText interest = (EditText) findViewById(R.id.IR_field);
public EditText pvalue;
public EditText ivalue;
private double mPvalue = 0;
private double mIvalue = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sicalculator);
    bar = (SeekBar)findViewById(R.id.seekBar);
    bar.setOnSeekBarChangeListener(this);
    pvalue = (EditText) principal.getText();
    ivalue = (EditText) interest.getText();
    String s = principal.getText().toString();
    mPvalue = Double.parseDouble(s);
    String s2 = interest.getText().toString();
    mIvalue = Double.parseDouble(s2);

}
    @Override
    public void onProgressChanged (SeekBar seekBar,int i, boolean b){
        years = i;
        YT = (TextView) findViewById(R.id.Years);
        YT.setText(years + " Year(s)");

    }

    @Override
    public void onStartTrackingTouch (SeekBar seekBar){

    }

    @Override
    public void onStopTrackingTouch (SeekBar seekBar){

    }

@Override
public void onClick(View view) {
    TextView fTextView = (TextView) findViewById(R.id.finalText);
    double finValue = mPvalue * (mIvalue/100) * years;
    fTextView.setText("The interest for " + pvalue + "at a rate of " +    ivalue + "for " + years + "year(s) is " + finValue);
}
}

You can't instantiate view variables by calling findViewById as you're declaring them. You have to declare them first and then instantiate either in onCreate or some method invoked after the Activity is bound with the view. Make sure to do it after setContentView(R.layout.sicalculator);

Okay, after seeing your layout, stacktrace and reading more in to your code, I saw that there are fundamental issues which ought to give you more crashes, so let's fix them.

First, pvalue and ivalue variables are unnecessary! Remove them. Related: You cannot assign an Editable to an EditText . So this line is invalid ivalue = (EditText) interest.getText(); Because getText() returns an Editable . But these are all redundant and unnecessary anyways.

Second, in onCreate method, let's just initialize views and not try to get values and parse them yet; the user (or you) haven't interacted nor entered any values there yet; so trying to parse null values in to Doubles will crash your app. Your onCreate method should look something like this. (Note that I'm also initializing your button and setting the click listener here).

@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.sicalculator);
  principal = (EditText) findViewById(R.id.PA_field);
  interest =  (EditText) findViewById(R.id.IR_field);
  bar = (SeekBar) findViewById(R.id.seekBar);
  bar.setOnSeekBarChangeListener(this);
  calcBtn = (Button)findViewById(R.id.calc_btn);
  calcBtn.setOnClickListener(this);
}

Now, you should get the values and parse them in your onClick listener - only when the user has entered values and clicked on the Calculate button, so your onClick() method should look something like this:

@Override
public void onClick(View view)
{
  TextView fTextView = (TextView) findViewById(R.id.finalText);
  mPvalue = Double.valueOf(principal.getText().toString());
  mIvalue = Double.valueOf(interest.getText().toString());

  double finValue = mPvalue * (mIvalue / 100) * years;
  fTextView.setText("The interest for " + mPvalue + "at a rate of " + mIvalue + "for " + years + "year(s) is " + finValue);
}

And that should clear the logic and order or declaring, initializing, retrieving values from variables for you. I hope this explains how Java and Android basics work.

Oh by the way, I actually ran the code and it's running on my phone so this isn't just off the top of my head. If this helped you, please accept the answer.

Best wishes,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM