Question

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
Was it helpful?

Solution 2

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);
        }
    });

OTHER TIPS

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

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

Hope this helps.

Your radio button declaration was wrong:

do like this;

RadioButton correctChoice1;

correctChoice1 = (RadioButton).findViewById(R.id.correct1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top