-1

I'm making an Android application that (is supposed to):
1. Gets the value of a Spinner on button click.
2. If the value is 2, make a random number between 1 and 2
3. If it is 1, set TextView text into "A"
4. If it is 2, set TextView text into "B"

I get the error "NullPointerException"

My code is here:

package org.infinitech.iguess.app;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends ActionBarActivity{
    public Button button;
    public TextView text=(TextView)findViewById(R.id.answer);
    public Spinner a=(Spinner)findViewById(R.id.choices);
    public String b;
    public int d;
    public Random r=new Random();
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);
        text=(TextView)findViewById(R.id.textBox);
        button.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                b=(String)a.getSelectedItem();
                if(b=="2"){
                    d=2+r.nextInt(1);
                    if(d==1){
                        text.setText("A");
                    }
                    else if(d==2){
                        text.setText("B");
                    }
                }
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int id=item.getItemId();
        if(id==R.id.action_settings){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
Kyle Horkley
  • 911
  • 1
  • 9
  • 23
  • Did you have a question? Recommended reading: http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist – Kevin Workman Jun 12 '14 at 16:09
  • @Kevin Workman Sorry it is now added. – Kyle Horkley Jun 12 '14 at 16:11
  • `if(b=="2"){` doesn't look good. Take a look at [how-do-i-compare-strings-in-java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Jun 12 '14 at 16:13
  • What line is your error on? Where is your full stacktrace? Did you read the link I already gave you? – Kevin Workman Jun 12 '14 at 16:14
  • @Kevin Workman I am no longer getting "NullPointerException", but the app is crashing every time I try to run it. – Kyle Horkley Jun 12 '14 at 16:16
  • You're calling findViewById in the class members, before onCreate is called. That can cause problems since views haven't been generated yet, so findViewById may return null. Just try to move the code public TextView text=(TextView)findViewById(R.id.answer); public Spinner a=(Spinner)findViewById(R.id.choices); to the onCreate method. Moreover, you first declared the variable "text" using findViewById(R.id.answer) and then using R.id.textbox. That's really weid. Finally, if you have a null pointer exception, the stacktrace will always tell you in which line that occurred, so take a look – Miquel Jun 12 '14 at 16:18
  • @Miquel Thank you so much that worked! I changed public into final as well. – Kyle Horkley Jun 12 '14 at 16:24

1 Answers1

0

You're assigning the views before the onCreate is called, move them inside the onCreate method. Also, change your if statement to use .equals instead of == as you're comparing Strings.

Another problem in your code is that you're assigning 2 different TextViews to the same TextView object (both R.id.answer and R.id.textBox). I removed the second assignment in the code below.

import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends ActionBarActivity{
    public Button button;
    public TextView text;
    public Spinner a;
    public String b;
    public int d;
    public Random r=new Random();
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        text = (TextView)findViewById(R.id.answer);
        a=(Spinner)findViewById(R.id.choices);
        button=(Button)findViewById(R.id.button);

        setContentView(R.layout.activity_main);
        button.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                b=(String)a.getSelectedItem();
                if(b.equals("2")){
                    d=2+r.nextInt(1);
                    if(d==1){
                        text.setText("A");
                    }
                    else if(d==2){
                        text.setText("B");
                    }
                }
            }
        });
    }
  • Actually textBox is a different TextView that will not be changed. It is the instructions. – Kyle Horkley Jun 12 '14 at 16:36
  • If you don't change it programmatically then why include it in the code? Try my code and tell if it worked. –  Jun 12 '14 at 16:37
  • I got rid of that textBox variable. You're right that if it doesn't need changed programmatically it shouldn't be in the Java code. – Kyle Horkley Jun 12 '14 at 16:46