I keep getting a NullPointerException but can't find where I made the mistake

Go To StackoverFlow.com

0

As far as I can tell, every variable that I use is not used without being given a value first. The nullpointerexception happens at the moment this activity begins.. Apologies that I cannot be more specific.

package pd.wilson.com;

import android.app.Activity;
import android.content.Intent;
 import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class Page3 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page3);
        final RadioGroup size = (RadioGroup)findViewById(R.id.RGSize);
        final RadioGroup crust = (RadioGroup)findViewById(R.id.RGCrust);
        final RadioButton small = (RadioButton)findViewById(R.id.RBS);
        final RadioButton medium = (RadioButton)findViewById(R.id.RBM);
        final RadioButton large = (RadioButton)findViewById(R.id.RBL);
        final RadioButton regular = (RadioButton)findViewById(R.id.RBRegular);
        final RadioButton thin = (RadioButton)findViewById(R.id.RBThin);

        final String sizeS, crustS;
        if (size.getCheckedRadioButtonId() == small.getId()){
            sizeS = "Small";
            }
            else if(size.getCheckedRadioButtonId() == medium.getId()){
            sizeS = "Medium";
            }   
            else{
            sizeS = "Large";
            }
        if (crust.getCheckedRadioButtonId() == regular.getId()){
            crustS = "Regular";
            }
            else{
            crustS = "Thin";
            }

        TextView size1 = (TextView)findViewById(R.id.tvsize);
        size1.setText(sizeS);
        TextView crust1 = (TextView)findViewById(R.id.tvcrust);
        crust1.setText(crustS);


            final CheckBox sausage = (CheckBox)findViewById(R.id.cbsausage);
                int i1 = Ifchecked.gettrue(sausage);
            final CheckBox mushrooms = (CheckBox)findViewById(R.id.cbmushrooms);
                int i2 = Ifchecked.gettrue(mushrooms);
            final CheckBox pepperoni = (CheckBox)findViewById(R.id.cbpepperoni);
                int i3 = Ifchecked.gettrue(pepperoni);
            final CheckBox onions = (CheckBox)findViewById(R.id.cbonions);
                int i4 = Ifchecked.gettrue(onions);
            final CheckBox ham = (CheckBox)findViewById(R.id.cbham);
                int i5 = Ifchecked.gettrue(ham);
            final CheckBox peppers = (CheckBox)findViewById(R.id.cbpeppers);
                int i6 = Ifchecked.gettrue(peppers);
            final CheckBox beef = (CheckBox)findViewById(R.id.cbbeef);
                int i7 = Ifchecked.gettrue(beef);
            final CheckBox tomatoes = (CheckBox)findViewById(R.id.cbtomatoes);
                int i8 = Ifchecked.gettrue(tomatoes);
            final CheckBox bacon = (CheckBox)findViewById(R.id.cbbacon);
                int i9 = Ifchecked.gettrue(bacon);
            final CheckBox green = (CheckBox)findViewById(R.id.cbgreen);
                int i10 = Ifchecked.gettrue(green);
            final CheckBox olives = (CheckBox)findViewById(R.id.cbolives);
                int i11 = Ifchecked.gettrue(olives);
    int numtop = i1+i2+i3+i4+i5+i6+i7+i8+i9+i10+i11;


}
}  

Stack Trace:

04-04 01:41:46.536: D/AndroidRuntime(676): Shutting down VM 
04-04 01:41:46.536: W/dalvikvm(676): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
04-04 01:41:46.556: E/AndroidRuntime(676): FATAL EXCEPTION: main 
04-04 01:41:46.556: E/AndroidRuntime(676): java.lang.RuntimeException: Unable to start activity ComponentInfo{pd.wilson.com/pd.wilson.com.Page3}: java.lang.NullPointerException 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.os.Looper.loop(Looper.java:123) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-04 01:41:46.556: E/AndroidRuntime(676): at java.lang.reflect.Method.invokeNative(Native Method) 
04-04 01:41:46.556: E/AndroidRuntime(676): at java.lang.reflect.Method.invoke(Method.java:507) 
04-04 01:41:46.556: E/AndroidRuntime(676): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-04 01:41:46.556: E/AndroidRuntime(676): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-04 01:41:46.556: E/AndroidRuntime(676): at dalvik.system.NativeStart.main(Native Method) 
04-04 01:41:46.556: E/AndroidRuntime(676): Caused by: java.lang.NullPointerException 
04-04 01:41:46.556: E/AndroidRuntime(676): at pd.wilson.com.Page3.onCreate(Page3.java:29) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-04 01:41:46.556: E/AndroidRuntime(676): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
04-04 01:41:46.556: E/AndroidRuntime(676): ... 11 more 
04-04 01:41:48.256: I/Process(676): Sending signal. PID: 676 SIG: 9 
04-04 01:45:18.077: D/AndroidRuntime(733): Shutting down VM 
04-04 01:45:18.077: W/dalvikvm(733): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
04-04 01:45:18.099: E/AndroidRuntime(733): FATAL EXCEPTION: main 
04-04 01:45:18.099: E/AndroidRuntime(733): java.lang.RuntimeException: Unable to start activity ComponentInfo{pd.wilson.com/pd.wilson.com.Page3}: java.lang.NullPointerException 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.os.Looper.loop(Looper.java:123) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-04 01:45:18.099: E/AndroidRuntime(733): at java.lang.reflect.Method.invokeNative(Native Method) 
04-04 01:45:18.099: E/AndroidRuntime(733): at java.lang.reflect.Method.invoke(Method.java:507) 
04-04 01:45:18.099: E/AndroidRuntime(733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-04 01:45:18.099: E/AndroidRuntime(733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-04 01:45:18.099: E/AndroidRuntime(733): at dalvik.system.NativeStart.main(Native Method) 
04-04 01:45:18.099: E/AndroidRuntime(733): Caused by: java.lang.NullPointerException 
04-04 01:45:18.099: E/AndroidRuntime(733): at pd.wilson.com.Page3.onCreate(Page3.java:29) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-04 01:45:18.099: E/AndroidRuntime(733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
04-04 01:45:18.099: E/AndroidRuntime(733): ... 11 more 
04-04 01:46:10.457: D/AndroidRuntime(767): Shutting down VM 
04-04 01:46:10.457: W/dalvikvm(767): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
04-04 01:46:10.496: E/AndroidRuntime(767): FATAL EXCEPTION: main 
04-04 01:46:10.496: E/AndroidRuntime(767): java.lang.RuntimeException: Unable to start activity ComponentInfo{pd.wilson.com/pd.wilson.com.Page3}: java.lang.NullPointerException 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.os.Looper.loop(Looper.java:123) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-04 01:46:10.496: E/AndroidRuntime(767): at java.lang.reflect.Method.invokeNative(Native Method) 
04-04 01:46:10.496: E/AndroidRuntime(767): at java.lang.reflect.Method.invoke(Method.java:507) 
04-04 01:46:10.496: E/AndroidRuntime(767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-04 01:46:10.496: E/AndroidRuntime(767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-04 01:46:10.496: E/AndroidRuntime(767): at dalvik.system.NativeStart.main(Native Method) 
04-04 01:46:10.496: E/AndroidRuntime(767): Caused by: java.lang.NullPointerException 
04-04 01:46:10.496: E/AndroidRuntime(767): at pd.wilson.com.Page3.onCreate(Page3.java:29) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-04 01:46:10.496: E/AndroidRuntime(767): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
04-04 01:46:10.496: E/AndroidRuntime(767): ... 11 more 
04-04 01:46:12.348: I/Process(767): Sending signal. PID: 767 SIG: 9 
04-04 01:55:31.597: D/AndroidRuntime(800): Shutting down VM 
04-04 01:55:31.597: W/dalvikvm(800): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
04-04 01:55:31.607: E/AndroidRuntime(800): FATAL EXCEPTION: main 
04-04 01:55:31.607: E/AndroidRuntime(800): java.lang.RuntimeException: Unable to start activity ComponentInfo{pd.wilson.com/pd.wilson.com.Page3}: java.lang.NullPointerException 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.os.Looper.loop(Looper.java:123) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-04 01:55:31.607: E/AndroidRuntime(800): at java.lang.reflect.Method.invokeNative(Native Method) 
04-04 01:55:31.607: E/AndroidRuntime(800): at java.lang.reflect.Method.invoke(Method.java:507) 
04-04 01:55:31.607: E/AndroidRuntime(800): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-04 01:55:31.607: E/AndroidRuntime(800): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-04 01:55:31.607: E/AndroidRuntime(800): at dalvik.system.NativeStart.main(Native Method) 
04-04 01:55:31.607: E/AndroidRuntime(800): Caused by: java.lang.NullPointerException 
04-04 01:55:31.607: E/AndroidRuntime(800): at pd.wilson.com.Page3.onCreate(Page3.java:29) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-04 01:55:31.607: E/AndroidRuntime(800): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
04-04 01:55:31.607: E/AndroidRuntime(800): ... 11 more 
04-04 01:55:33.357: I/Process(800): Sending signal. PID: 800 SIG: 9 
2012-04-04 01:51
by Wilson
You have a stacktrace, don't you? What is the output from logcat - Travis Webb 2012-04-04 01:53
@TravisWebb http://www.text-upload.com/read.php?id=331977&c=580142 - Wilson 2012-04-04 01:56
Stack trace in the question please.Whymarrh 2012-04-04 02:06
@Jesus, thanks for the stack trace, but in the future please include it in your question directly if at all possible. (who knows how long text-upload.com will be around? we want this question useful for as long as SO exists - Kirk Woll 2012-04-04 02:07
can we have xml file to - Shankar Agarwal 2012-04-04 02:38
@Agarwal Why? What use would that be - user207421 2012-04-05 01:06


2

Based on your stack trace:

04-04 01:41:46.556: E/AndroidRuntime(676): Caused by: java.lang.NullPointerException 
04-04 01:41:46.556: E/AndroidRuntime(676): at pd.wilson.com.Page3.onCreate(Page3.java:29) 

it looks like line 29 of that file is where the problem lies:

if (size.getCheckedRadioButtonId() == small.getId()){

Either size or small is likely to be a null reference so you should check that.

If it turns out one of them is null (likely), a google for android findviewbyid return null turns up quite a few potential causes.

2012-04-04 02:03
by paxdiablo
+1 for having the fortitude, which I lacked, to copy and paste his code into an editor to determine what is on line 29 - Travis Webb 2012-04-04 02:05


1

According to your stacktrace:

Caused by: java.lang.NullPointerException
E/AndroidRuntime(676): at pd.wilson.com.Page3.onCreate(Page3.java:29)

Therefore, I would check line 29 of Page3.java. The NullPointerException is thrown from that line, so you should look at that line and try to deduce what variable you are dereferencing (dereference operator = .)

2012-04-04 01:58
by Travis Webb
This should be a comment - Ry- 2012-04-04 01:59
He obviously doesn't know how to read a stacktrace, so this is an answer - Travis Webb 2012-04-04 02:02
As far as I can tell, the radiobutton/radiogroups have values.. Is it possible that because the radiobuttons and groups are on an xml document that is viewed during a different activity that I am no longer able to reference them - Wilson 2012-04-04 02:05
+1 for complimenting paxdiablo's effort - Win Myo Htet 2012-04-04 02:37


0

   final String sizeS, crustS;
        if (size.getCheckedRadioButtonId() == small.getId()){
            sizeS = "Small";
            }
            else if(size.getCheckedRadioButtonId() == medium.getId()){
            sizeS = "Medium";
            }   
            else{
            sizeS = "Large";
            }
        if (crust.getCheckedRadioButtonId() == regular.getId()){
            crustS = "Regular";
            }
            else{
            crustS = "Thin";
            }

Looking at above lines you are declarring sizeS as final that means that it cant be changed again . however that is not the problem for crash..

Either size.getCheckedRadioButtonId() or small.getId() is null.

2012-04-04 02:39
by Shankar Agarwal
better if you can provide your xml file to - Shankar Agarwal 2012-04-04 02:40