zkminusck
zkminusck

Reputation: 11

Button click problem

I'm a beginner in application development. My problem is, that when I run my app and I click on the Calculate button, the program stops. The code:

public class screen1 extends Activity {
    private EditText name; 
    private CheckBox box1;
    private Spinner spinner;
    private TextView text1, text2, text3, text4, text5, text6;
    private Button calcbutton, closebutton;
    String strength;  

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner hubSpinner = (Spinner) findViewById(R.id.myspinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.military_ranks , android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        hubSpinner.setAdapter(adapter);

        name = (EditText)findViewById(R.id.editText1);      
        strength = name.getText().toString();  

        box1 = (CheckBox)findViewById(R.id.checkBox1);

        text1 = (TextView)findViewById(R.id.textView4);
        text2 = (TextView)findViewById(R.id.textView6);
        text3 = (TextView)findViewById(R.id.textView8);
        text4 = (TextView)findViewById(R.id.textView10);
        text5 = (TextView)findViewById(R.id.textView12);
        text6 = (TextView)findViewById(R.id.textView14);

        final Button calcbutton = (Button) findViewById(R.id.button1);
        calcbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int str = Integer.valueOf(strength);
                int rank = spinner.getSelectedItemPosition()+1;
                double sebzes;
                if(box1.isChecked()){
                    sebzes = (((rank-1)/20+0.3)*((str/10)+40))*1*(1+1/100);
                    text1.setText(Double.toString(sebzes));
                }
                else{
                    sebzes = (((rank-1)/20+0.3)*((str/10)+40))*1;
                    text1.setText(Double.toString(sebzes));
                }
            }
        });

        final Button closebutton = (Button) findViewById(R.id.button2);
        closebutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
    }
}

In the edittext component you should be able to write numbers only. I don't know why it's not working.

Upvotes: 1

Views: 471

Answers (2)

MusiGenesis
MusiGenesis

Reputation: 75296

You're not instantiating spinner anywhere, but you're referencing it in the second line of your button click method. Probably a null reference problem.

Upvotes: 0

anon
anon

Reputation:

The problem are these two lines:

 int str = Integer.valueOf(strength);
 int rank = spinner.getSelectedItemPosition()+1;

The first won't fail if you use only numbers in your EditText but it would be better to ensure that or at least catch the exception that is thrown when you try to convert a character to a numberical value. Additionally you could also use Integer.valueOf(strength).intValue(); even so it is normally not really necessary.

The real problem is the second line. You declared the variable spinner but you never instantiate it. That's why you will get a NullPointerException there.

On an unrelated note: You also should start your class name with a capital letter to follow the Java naming conventions.

Upvotes: 1

Related Questions