-1

I am trying to build a simple calculator to learn Android programming. I've got my UI all sorted but the "Submit" button doesn't seem to be working, which should do logic on two user submitted values and output the result.

Here is the method:

@Override
public void onClick(View view){
    String num1 = etfirst.getText().toString();
    String num2 = etsecond.getText().toString();
    String amount = etamount.getText().toString();
    switch(view.getId()){
        case R.id.btnSubmit:
            int divide =  (Integer.parseInt(num2) / Integer.parseInt(num1)) +1;
            int multiply = divide * Integer.parseInt(amount);
            tvResult.setText(String.valueOf(multiply));
            break;
    }
}

I've declared all my variables before hand and initialized them. All variable names match but the ID of the elements in the UI so I'm not sure what the problem is.

Here is the logcat as requested: https://i.stack.imgur.com/6RurY.png. Posted to Imgur as 10 reputation is needed to post images.

  • 1
    And what is the problem? Is the calculated result wrong? Doesn't it enter the `case`? Isn't the method called? An exception? – Tom Aug 06 '15 at 21:22
  • Sorry for not specifying. The problem is that there is no output. It seems the calculation isn't carried out. – joe murphy Aug 06 '15 at 21:23
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Aug 06 '15 at 21:51
  • why would you post an image of your logcat? what is wrong with text? – njzk2 Aug 06 '15 at 21:51
  • hint: click on the link in the error, you'll see where it is. – njzk2 Aug 06 '15 at 21:52
  • 1
    The link brings me to : String num1 = etfirst.getText().toString();. In the activity_main the number field is etFirst so I tried refactoring but that had no affect. – joe murphy Aug 06 '15 at 22:06

3 Answers3

0
   public class MainActivity extends ActionBarActivity implements View.OnClickListener{
    private Button btnSubmit;
    private TextView tvResult;
    private EditText etfirst, etsecond, etamount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSubmit = (Button) findViewById(R.id.btnSubmit);
        tvResult = (TextView) findViewById(R.id.tvResult);
        etfirst = (EditText) findViewById(R.id.etFirst);
        etsecond = (EditText) findViewById(R.id.etSecond);
        etamount = (EditText) findViewById(R.id.etAmount);
        btnSubmit.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.btnSubmit:

            if(etfirst.getText().toString().length()==0){
                Toast.makeText(getBaseContext(), "First edittext empty", Toast.LENGTH_LONG).show();
                }
            else if(etsecond.getText().toString().length()==0){
                Toast.makeText(getBaseContext(), "Second edittext empty", Toast.LENGTH_LONG).show();
                }
            else if(etamount.getText().toString().length()==0){
                Toast.makeText(getBaseContext(), "Amount edittext empty", Toast.LENGTH_LONG).show();
                }
            else{
            int divide = (Integer.parseInt(etsecond.getText().toString()) / Integer.parseInt(etfirst.getText().toString())) + 1;
            int multiply = divide * Integer.parseInt(etamount.getText().toString());
            tvResult.setText(String.valueOf(multiply));
            }
            break;
        }
    }

}
Aakash
  • 5,181
  • 5
  • 20
  • 37
  • When I run the app with this line, it crashes when I press the submit button. – joe murphy Aug 06 '15 at 21:26
  • i have edited my answer, if it still crashes, please post your logcat. – Aakash Aug 06 '15 at 21:28
  • Now the app crashes as soon as I run it. I'll add the logcat to the OP. – joe murphy Aug 06 '15 at 21:30
  • Here is the full code: https://gist.github.com/adhorrig/052771c8150ba5d5871c there was too much for it to go into the OP without adding more text. – joe murphy Aug 06 '15 at 21:36
  • 1
    Remove this: findViewById(R.id.btnSubmit).setOnClickListener(this); and do this: buttonSubmit=(Button) findViewById(R.id.btnSubmit); buttonSubmit.setOnClickListener(this); – Aakash Aug 06 '15 at 21:44
  • When I do this, it goes back to crashing when I press the submit button. I can put the project on github if you'd like? – joe murphy Aug 06 '15 at 21:51
  • Edited my answer, just paste my code, it will hopefully work and if not you can put it on github. – Aakash Aug 06 '15 at 21:52
  • The same issue persists, I've added the project to Github in the OP. – joe murphy Aug 06 '15 at 22:00
  • There were too many errors in your code, i have proposed change in your code on github, check now and let me know soon. – Aakash Aug 06 '15 at 22:20
  • Since you removed private Button btnSubmit; I know get errors when btnSubmit is referenced as it cannot be resolved. ~Only in the onCreate method. – joe murphy Aug 06 '15 at 22:28
  • you are very new to android development, i guess. Check my edited answer :) – Aakash Aug 06 '15 at 22:34
  • Yes, this is my first day with android development but I've been using Java for some time now. It now works so thank you! I'm still unsure what the problem is though. You seem to have added error handling but not much else? Could you give a recap in your answer of what the problem was? :) – joe murphy Aug 06 '15 at 22:41
  • Your code was vulnerable to exceptions, invalid int, null pointer exception and much more. You can check your earlier code and find out the exceptions in logcat where were you getting exceptions. You need to clear basics of android development and you can refer to android documentation then after start coding. Also if my answer helped, mark it as correct as it took too much for me to make things corect – Aakash Aug 06 '15 at 22:45
0

From the explanation, and the Logs, I can clearly see that it is a null pointer problem; may be caused either by not getting a correct reference to the button or the text view.

In your onCreate make sure you have this:

Button button = (Button) findViewById(R.id.enter_button);
button.setOnclickListener(this);

Make sure the resource Id from the XML file matches your reference.
And for the textView where you display the output, get a correct reference like

TextView theText = (TextView) findViewById(R.id.the_text);

Again make sure you have a correct XML resource ID (the reference)
Then the null pointer should be solved.

Double check your whole code and make sure your are getting a correct reference for the button and textView.

zed
  • 2,298
  • 4
  • 27
  • 44
CodeDaily
  • 756
  • 4
  • 17
0

Try this:

Button btnSubmit;
TextView tvResult;
EditText etfirst,etsecond,etamount;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    tvResult = (TextView)findViewById(R.id.tvResult);
    etfirst = (EditText)findViewById(R.id.etFirst);
    etsecond = (EditText)findViewById(R.id.etSecond);
    etamount = (EditText)findViewById(R.id.etAmount); //You wasnt definind this in the original code
    btnSubmit.setOnClickListener(this);
}





@Override
public void onClick(View view){
    String num1 = etfirst.getText().toString();
    String num2 = etsecond.getText().toString();
    String amount = etamount.getText().toString();
    switch(view.getId()){
        case R.id.btnSubmit:
            int divide =  (Integer.parseInt(num2) / Integer.parseInt(num1)) +1;
            int multiply = divide * Integer.parseInt(amount);
            tvResult.setText(String.valueOf(multiply));
            break;
    }
}
Berry Berry
  • 198
  • 9
  • This works. Was the only problem that I missed out on defining etamount? – joe murphy Aug 06 '15 at 22:42
  • In android you need to definy all the UI elements inside the onCreate, you wasnt defining etamount and calling it in the onClick, those were the main problems. – Berry Berry Aug 06 '15 at 22:45
  • I dont remember very well the first code, the second one that gave you Aakash, was extremly bad. The first one had that problem, and i remember that you define the variable button two times, one inside the onCreate and the second one at the beginning of the class. – Berry Berry Aug 06 '15 at 22:51