0

I have an app that is supposed to:
1. If Spinner value is "1", do a certain math equation from EditText value
2. If Spinner value is "2", do a certain math equation from EditText value I'm getting "NullPointerException"
Here is my code:

package org.infinitech.degreescalculator.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity{
    public Button button=(Button)findViewById(R.id.button);
    public String selectedItem;
    Integer number;
    Integer a;
    String numberTwo;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText input=(EditText)findViewById(R.id.input);
        final TextView answerText=(TextView)findViewById(R.id.answer);
        final Spinner spinner=(Spinner)findViewById(R.id.options);
            final String inputTwo=(String)getText(R.id.input);
    selectedItem=(String)spinner.getSelectedItem();
        button.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                if(selectedItem.equals("Fahrenheit to Celsius")){
                    a=Integer.parseInt(inputTwo);
                    number=a*100;
                    numberTwo=number.toString();
                    answerText.setText(numberTwo);
                }
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        return super.onOptionsItemSelected(item);
    }
}
Kyle Horkley
  • 579
  • 1
  • 4
  • 16
  • Welcome to StackOverflow! We are here to help when you have a specific question. I suggest that you fire up a debugger to help you narrow down the problem. After debugging, if you still can't figure out how to fix it, feel free to ask more questions. – Code-Apprentice Jun 14 '14 at 19:39
  • possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Code-Apprentice Jun 14 '14 at 19:40

3 Answers3

0

You can get the value as a String like this:

editTextObject.getText().toString()
Martin Dinov
  • 8,190
  • 2
  • 26
  • 37
0

Actually this line final String inputTwo=(String)getText(R.id.input); will not give any value so instead doing it like that just directly get the text of it inside the onclick of the button.

Change this:

a=Integer.parseInt(inputTwo);

to:

a=Integer.parseInt(input.getText().toString());
Rod_Algonquin
  • 25,268
  • 6
  • 47
  • 61
0

Your problem is that selectedItem is assigned in your onCreate method, which runs before any of the items in your spinner have been selected, and is therefore always null. Move the line that assigns it to inside your button click listener's onClick method, and it should solve the problem.

Similarly, you may also need to move your inputTwo variable into the onClick method, so this too is not set at creation time and never updated.

Jules
  • 13,417
  • 7
  • 75
  • 117