0

Please help I am almost done with my code, but when I run it I get nullpointerexception.. I am trying to increment a static int variable on button click, their are five activities. In each activity their is a question, when answered correctly the static int must increment by 1. when last page is reached.. I display the total..

package com.example.kei;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;

public class MainActivity extends Activity {


    RadioButton correctChoice1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        correctChoice1.findViewById(R.id.correct1);
        correctChoice1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                AppSingleton.score++;
                Intent intent = new Intent(MainActivity.this, Page_two.class);
                startActivity(intent);
            }
        });
    }

    public static class AppSingleton {

        public static int score= 0;
    }

    public void choice_a(View view){
        Intent intent = new Intent(this, Page_two.class);
        startActivity(intent);
    }



    public void choice_c(View view){
        Intent intent = new Intent(this, Page_two.class);
        startActivity(intent);
    }

}

in the code choice_a and choice_c are wrong answers. i try to increment the correct answer with onlicklistener for radiobutton. why cant the code execute and give null pointer exception? please help.. i dont know what to do now..

LOGCAT:

03-25 05:21:04.811: D/dalvikvm(1052): Late-enabling CheckJNI
03-25 05:21:04.827: E/Trace(1052): error opening trace file: No such file or directory (2)
03-25 05:21:04.863: D/AndroidRuntime(1052): Shutting down VM
03-25 05:21:04.863: W/dalvikvm(1052): threadid=1: thread exiting with uncaught exception (group=0xa6246288)
03-25 05:21:04.867: E/AndroidRuntime(1052): FATAL EXCEPTION: main
03-25 05:21:04.867: E/AndroidRuntime(1052): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kei/com.example.kei.MainActivity}: java.lang.NullPointerException
03-25 05:21:04.867: E/AndroidRuntime(1052):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at android.os.Looper.loop(Looper.java:137)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at java.lang.reflect.Method.invoke(Method.java:511)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at dalvik.system.NativeStart.main(Native Method)
03-25 05:21:04.867: E/AndroidRuntime(1052): Caused by: java.lang.NullPointerException
03-25 05:21:04.867: E/AndroidRuntime(1052):     at com.example.kei.MainActivity.onCreate(MainActivity.java:18)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at android.app.Activity.performCreate(Activity.java:5008)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-25 05:21:04.867: E/AndroidRuntime(1052):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-25 05:21:04.867: E/AndroidRuntime(1052):     ... 11 more
03-25 05:33:23.686: E/Trace(1694): error opening trace file: No such file or directory (2)
03-25 05:33:23.718: D/AndroidRuntime(1694): Shutting down VM
03-25 05:33:23.718: W/dalvikvm(1694): threadid=1: thread exiting with uncaught exception (group=0xa6246288)
03-25 05:33:23.722: E/AndroidRuntime(1694): FATAL EXCEPTION: main
03-25 05:33:23.722: E/AndroidRuntime(1694): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kei/com.example.kei.MainActivity}: java.lang.NullPointerException
03-25 05:33:23.722: E/AndroidRuntime(1694):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at android.os.Looper.loop(Looper.java:137)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at java.lang.reflect.Method.invoke(Method.java:511)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at dalvik.system.NativeStart.main(Native Method)
03-25 05:33:23.722: E/AndroidRuntime(1694): Caused by: java.lang.NullPointerException
03-25 05:33:23.722: E/AndroidRuntime(1694):     at com.example.kei.MainActivity.onCreate(MainActivity.java:23)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at android.app.Activity.performCreate(Activity.java:5008)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-25 05:33:23.722: E/AndroidRuntime(1694):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-25 05:33:23.722: E/AndroidRuntime(1694):     ... 11 more
14
  • Just remove the static from the AppSingleton class. Simple create class and make its variable static only. Commented Mar 25, 2014 at 5:42
  • correctChoice1 is not initialized. Replace correctChoice1.findViewById(R.id.correct1); with correctChoice1 = (RadioButton) findViewById(R.id.correct1); Commented Mar 25, 2014 at 5:42
  • Radiobutton TypeCasting error. Commented Mar 25, 2014 at 5:43
  • thank you sirs for the answers i appreciate it very much, im a beginner with android thank you!! ill try this thanks again Commented Mar 25, 2014 at 5:46
  • @GrIsHu pls help when i remove the static the AppSingleton.score++; give an error saying remove final.. i remove it and it says remove static, the error just goes in circles Commented Mar 25, 2014 at 5:56

3 Answers 3

1

Replace correctChoice1.findViewById(R.id.correct1); with

correctChoice1 = (RadioButton )findViewById(R.id.correct1);

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to initialize your RadioButton as below:

 correctChoice1= (RadioButton )findViewById(R.id.correct1);
    correctChoice1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
          if(AppSingleton.score <=5)
            AppSingleton.score++;
            Intent intent = new Intent(MainActivity.this, Page_two.class);
            startActivity(intent);
        }
    });

Comments

0

Your radio button declaration was wrong:

do like this;

RadioButton correctChoice1;

correctChoice1 = (RadioButton).findViewById(R.id.correct1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.