-1

I want to print user score in the textview in end_page.xml. I can do it in activity_main.xml but when i change the layout i see this error. I think I am seeing this error because of scoreEndBox.setText(scoreNum + "");. How can I solve it?

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_page);
    }

    public Question question;
    public GameTimer gameTimer;
    public int scoreNum = 0;

    public void StartGame(View v){
        setContentView(R.layout.activity_main);
        ShowQuestion();
    }

    public void ShowQuestion(){
        question = new Question();
        int[] choices = new int[4];

        TextView questionBox = (TextView)findViewById(R.id.questionBox);
        Button aChoice = (Button)findViewById(R.id.a);
        Button bChoice = (Button)findViewById(R.id.b);
        Button cChoice = (Button)findViewById(R.id.c);
        Button dChoice = (Button)findViewById(R.id.d);

        Random r = new Random();
        int var = r.nextInt(3);

        question.Questioner();

        String showedQuestion = question.question;
        choices[0] = question.selection1;
        choices[1] = question.selection2;
        choices[2] = question.selection3;
        choices[3] = question.selection4;
        choices[var] = question.answer;

        questionBox.setText(showedQuestion);
        aChoice.setText(choices[0] + "");
        bChoice.setText(choices[1] + "");
        cChoice.setText(choices[2] + "");
        dChoice.setText(choices[3] + "");
    }

    public void Score(){
        TextView score = (TextView)findViewById(R.id.score);
        scoreNum = scoreNum + 55;
        score.setText(scoreNum + "");
    }

    public void AnswerChecker(View v){
        Button userChoice = (Button)findViewById(v.getId());
        String userAnswer = userChoice.getText().toString();
        //TextView questionBox = (TextView)findViewById(R.id.questionBox);
        TextView scoreEndBox = (TextView)findViewById(R.id.scoreEnd);
        int userAnswerInt = Integer.parseInt(userAnswer);
        int rightAnswerInt = question.answer;

        if (userAnswerInt == rightAnswerInt){
            ShowQuestion();
            Score();
        } else {
            //questionBox.setText(R.string.wrong_answer);
            //scoreEndBox.setText(scoreNum + "");
            setContentView(R.layout.end_page);
            scoreEndBox.setText(scoreNum + "");
            scoreNum = 0;
        }
    }
}

Error:

10-16 20:39:58.070    2640-2640/com.ebook.hco.mindcalc E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ebook.hco.mindcalc, PID: 2640
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3823)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.ebook.hco.mindcalc.MainActivity.AnswerChecker(MainActivity.java:102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
ligi
  • 39,001
  • 44
  • 144
  • 244
hco
  • 655
  • 1
  • 5
  • 15
  • 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) – ben75 Oct 16 '14 at 21:08
  • can you tell me which is line 102 of MainActivity? – Angad Tiwari Oct 16 '14 at 21:13

1 Answers1

0

this is what i am conjuring-(i jux love this word.) lol -im thinking its because of this

else {
    //questionBox.setText(R.string.wrong_answer);
    //scoreEndBox.setText(scoreNum + "");
    setContentView(R.layout.end_page); //this is the reason
    scoreEndBox.setText(scoreNum + "");
    scoreNum = 0;
}

setcontentview from what i know resets everthing so what you have to do is instantiate it again and set the text and there wouldnt be any nullpointer exception

so do this

else {
    //questionBox.setText(R.string.wrong_answer);
    //scoreEndBox.setText(scoreNum + "");
    setContentView(R.layout.end_page);
    TextView scoreEndBox = (TextView)findViewById(R.id.scoreEnd);
    scoreEndBox.setText(scoreNum + "");
    scoreNum = 0;
}

and also make sure R.id.scoreEnd is in that layout.. let me know if it helps

Elltz
  • 10,730
  • 4
  • 31
  • 59
  • Yes it works when i changed name of the second scoreEndBox variable. The code is below: else { setContentView(R.layout.end_page); TextView scoreEndBox2 = (TextView)findViewById(R.id.scoreEnd); scoreEndBox2.setText(scoreNum + ""); scoreNum = 0; } thanks for help! – hco Oct 17 '14 at 21:41
  • great!! im glad it helped...btw, If it helped you then like or accept it as answer.. happy codding @user200306 – Elltz Oct 17 '14 at 21:49