Take the 2-minute tour ×

I'm new to android and trying to create a simple program that adds three inputs together and then prints the output to an EditText. Whenever I hit the button I'm getting a crash in line 63 (the last line I posted below) of my code... I think it could be due to a null object or something like that. I'm 99% sure all of my variables and everything else are declared correctly. Anyways, here's my code for onClick method and if anyone can figure out what's causing the crash I'd be much obliged.

Button solve = (Button) findViewById(R.id.solve);

solve.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {

        if  ((a.getText().length() == 0)
            || (" ".equals(a.getText().toString()))
            || (" ".equals(b.getText().toString()))
            || (" ".equals(c.getText().toString()))
            || (b.getText().length() == 0)
            || (c.getText().length() == 0)) {
Toast.makeText(getApplicationContext(), "Some of your inputs are empty.",  Toast.LENGTH_SHORT).show();

        } else {

            double sol = new Double(a.getText().toString())
            + new Double(b.getText().toString())
            + new Double(c.getText().toString());
            res1.setText(Double.toString(sol)); //line that is causing the crash
share|improve this question
Before you setText(). Try to print out your a, b, c and see if you are getting what you are suppose to be getting. –  wtsang02 Oct 24 '12 at 1:23
Just a tip, make sure that a, b, and c do not have any other charter's in them. –  The Floating Brain Oct 24 '12 at 2:54
Please paste the log cat here –  Nguyen Minh Binh Oct 24 '12 at 3:10
add comment

2 Answers

You are converting String values to Double in wrong way, see you can do it in following two ways,

  1. use Double.parseDouble()

    double sol = new Double.parseDouble((a.getText().toString().trim());     // also don't forget to use trim() method to remove white spaces 
  2. use new Double( value ).doubleValue()

    double sol = new Double(a.getText().toString().trim()).doubleValue();

Now coming to set this sol variable's value to EditText try following way,

  1. res1.setText((String)sol));
  2. res1.setText(String.valueOf(sol));
  3. res1.setText( sol + "" );
share|improve this answer
ok, I tried using the second method, but it still crashes at res1.setText(Double.toString(sol)); Should I change the way I go about this line as well? Sorry if I'm misunderstanding –  user1769938 Oct 24 '12 at 2:04
why you are not using trim() method ?, and what value you are passing in the text box ? –  Lucifer Oct 24 '12 at 2:05
ah, sorry, meant to include that: res1.setText(Double.toString(sol).trim()); Passing 1, 2, and 3 respectively. –  user1769938 Oct 24 '12 at 2:11
@user1769938, i have updated my answer, try it now –  Lucifer Oct 24 '12 at 2:12
add comment

You can try to use Double.Parse...


share|improve this answer
add comment

Your Answer


