Question

I try to open the same class as the one I am currently in with different parameters when pressing a button. I do this with:

Intent j = new Intent("com.test.com.ANSWER");
            id = calculateId(0);
            j.putExtra("id", Long.toString(id));
            startActivityForResult(j, 0);

But I get a nullpointer exception.. Is there some rule that you cannot open the same activity within itself?

Error:

12-01 01:29:10.314: E/AndroidRuntime(2194): FATAL EXCEPTION: main
12-01 01:29:10.314: E/AndroidRuntime(2194): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.Answer}: java.lang.NullPointerException
12-01 01:29:10.314: E/AndroidRuntime(2194):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at android.os.Looper.loop(Looper.java:130)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at java.lang.reflect.Method.invokeNative(Native Method)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at java.lang.reflect.Method.invoke(Method.java:507)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at dalvik.system.NativeStart.main(Native Method)
12-01 01:29:10.314: E/AndroidRuntime(2194): Caused by: java.lang.NullPointerException
12-01 01:29:10.314: E/AndroidRuntime(2194):     at com.test.Answer.onCreate(Answer.java:86)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-01 01:29:10.314: E/AndroidRuntime(2194):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
Was it helpful?

Solution

I think your id isn't getting initialized. You are trying to initialize it with a function that uses the id so it never has a value. If this isn't it then you are using the same variable name in different places which isn't very good practice usually

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top