0

I am beginner in android app development. My code is as follows- How to solve Number Format Exception in this code-

public class InterestActivity extends AppCompatActivity {

private EditText editAmount;
private EditText editInterest;
private EditText editDuration;

private TextView resultView;
private Button Calculate;

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

    editAmount = (EditText) findViewById(R.id.editAmount);
    editInterest = (EditText) findViewById(R.id.editInterest);
    editDuration = (EditText) findViewById(R.id.editDuration);
    resultView = (TextView) findViewById(R.id.resultView);
    Calculate = (Button) findViewById(R.id.button4);
}

    public void interestCalculate (View v) {
        resultView= (TextView) findViewById(R.id.resultView);
        Calculate = (Button) findViewById(R.id.button4);

        int a = Integer.parseInt(editAmount.getText().toString());
        int i = Integer.parseInt(editInterest.getText().toString());
        int d = Integer.parseInt(editDuration.getText().toString());

        int ir= i/100;

        float res = a*ir*d;
        int intres= (int) res;
    resultView.setText(Integer.toString(intres));
    }}
Rohit Surwase
  • 701
  • 5
  • 17

2 Answers2

0

You are assigning the Views to temporary local variables instead of the attributes of the class.

Short solution:

Remove EditText before editAmount = ...

Likewise with all the other assignments inside onCreate.

If you want to continue Android programming you should understand why this will work. This might help you: What is the difference between field, variable, attribute, and property in Java POJOs?

Community
  • 1
  • 1
F43nd1r
  • 7,690
  • 3
  • 24
  • 62
  • Thanks! Is there any other errors in this code because your solution solved null point exception but still there are some other errors. Thanks again. – Rohit Surwase Jan 10 '16 at 02:09
  • Instead of editing the question you should ask a new one if you have a new problem, so others with the same problem can find your old question. If you get a NumberformatException it's probably because there are no valid numbers in the editText boxes. The answer of @tiny sunlight shows how such a case can be handled. – F43nd1r Jan 10 '16 at 03:02
0
 public void interestCalculate (View v) {
        resultView= (TextView) findViewById(R.id.resultView);
        Calculate = (Button) findViewById(R.id.button4);

        int a = 0;
        int i = 0;
        int d = 0;
        try{
            a= Integer.parseInt(editAmount.getText().toString());
        }catch(Throwable e){
            Toast.makeText(getApplicationContext(),"editAmount"+ "'s value is not a Integer",Toast.LENGTH_LONG);
        }
        try{
            i = Integer.parseInt(editInterest.getText().toString());

        }catch(Throwable e){
            Toast.makeText(getApplicationContext(),"editInterest"+ "'s value is not a Integer",Toast.LENGTH_LONG);
        }
        try{
            d = Integer.parseInt(editDuration.getText().toString());
        }catch(Throwable e){
            Toast.makeText(getApplicationContext(),"editDuration"+ "'s value is not a Integer",Toast.LENGTH_LONG);
        }


        int ir= i/100;

        float res = a*ir*d;
        int intres= (int) res;
        resultView.setText(Integer.toString(intres));
    }}
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42