Null Pointer Exception

Search This thread

DarkMethod

New member
Feb 14, 2014
3
0
Hello there,
I am trying to learn android application development through one of the online tutorials .I am stuck at a point and since,both java and android are new for me I can't figure out how to get out of it.Here's the code to my Startingpoint.java:
Code:
public class Startingpoint extends Activity {
int counter;
Button add,sub;
TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        counter=0;
        add=(Button)findViewById(R.id.bAdd);
        sub=(Button)findViewById(R.id.bSub);
        display=(TextView)findViewById(R.id.display);
        add.setOnClickListener(new View.OnClickListener() {
                        
                        @Override
                        public void onClick(View v) {
                                counter++;
                                display.setText("Your total is " + counter);
                                
                        }
                });
        sub.setOnClickListener(new View.OnClickListener() {
                        
                        @Override
                        public void onClick(View v) {
                                counter--;
                                display.setText("Your total is" + counter);
                                
                        }
                });
    }
 
}
The following are the contents within the LineaLlayout in my main.xml file:
Code:
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/result"
        android:textSize="25sp"
        android:layout_gravity="fill_horizontal"
        android:gravity="center"
        android:id="@+id/display"
    />
    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@string/bAdd"
        android:layout_gravity="center"
        android:gravity="center"
		android:id="@+id/bAdd"    
    />
    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@string/bSub"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/bSub"
    />
And this is what the logcat has to say :
Code:
02-13 11:44:12.076: D/AndroidRuntime(2026): Shutting down VM
02-13 11:44:12.076: W/dalvikvm(2026): threadid=1: thread exiting with uncaught exception (group=0xb2d5bb08)
02-13 11:44:12.156: E/AndroidRuntime(2026): FATAL EXCEPTION: main
02-13 11:44:12.156: E/AndroidRuntime(2026): Process: com.thenewboston.rohit, PID: 2026
02-13 11:44:12.156: E/AndroidRuntime(2026): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thenewboston.rohit/com.thenewboston.rohit.Startingpoint}: java.lang.NullPointerException
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at android.os.Handler.dispatchMessage(Handler.java:102)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at android.os.Looper.loop(Looper.java:137)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at android.app.ActivityThread.main(ActivityThread.java:4998)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at java.lang.reflect.Method.invokeNative(Native Method)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at java.lang.reflect.Method.invoke(Method.java:515)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at dalvik.system.NativeStart.main(Native Method)
02-13 11:44:12.156: E/AndroidRuntime(2026): Caused by: java.lang.NullPointerException
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at com.thenewboston.rohit.Startingpoint.onCreate(Startingpoint.java:21)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at android.app.Activity.performCreate(Activity.java:5243)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-13 11:44:12.156: E/AndroidRuntime(2026): 	... 11 more
Being inexperienced in java , I could not figure out what's wrong with the code.May be the exception is due to my add,sub and display being null but I am not sure.Please help me figure out what's wrong with my code and how to deal with it.
Any help is greatly appreciated.
 

nikwen

Senior Member
Feb 1, 2013
3,142
1,597
Berlin, Germany
www.nikwen.de
Well, you can find the line where it crashes in the logcat:

Code:
02-13 11:44:12.156: E/AndroidRuntime(2026): Caused by: java.lang.NullPointerException
02-13 11:44:12.156: E/AndroidRuntime(2026): 	at com.thenewboston.rohit.Startingpoint.onCreate([B]Startingpoint.java:21[/B])

It's line 21. Which one is that?

Have a look at my debugging tutorial here as well, mainly the part on understanding the logcat. It will probably be very helpful and save much time: http://xdaforums.com/showthread.php?t=2325164 ;)
 

sak-venom1997

Senior Member
Feb 4, 2013
928
415
Lucknow
Well, you can find the line where it crashes in the logcat:

Code:
02-13 11:44:12.156: E/AndroidRuntime(2026): Caused by: java.lang.NullPointerException
02-13 11:44:12.156: E/AndroidRuntime(2026): at com.thenewboston.rohit.Startingpoint.onCreate([B]Startingpoint.java:21[/B])

It's line 21. Which one is that?

Have a look at my debugging tutorial here as well, mainly the part on understanding the logcat. It will probably be very helpful and save much time: http://xdaforums.com/showthread.php?t=2325164 ;)

I bet they must be the findViewById(int id);
as much i trust my counting skills line 21 is calling for one the views
i still remeber the first NPE when i started android :D :p

Sent from my GT-S5302 using Tapatalk 2
 
Last edited:

DarkMethod

New member
Feb 14, 2014
3
0
I am sorry , I should have mentioned it.
Line 21 -> add.setOnClickListener(new View.OnClickListener()
 

DarkMethod

New member
Feb 14, 2014
3
0
I have declared the ids for each view as you can see in the layout.xml attached.This NPE is turning out to be quite frustrating now.
 

sak-venom1997

Senior Member
Feb 4, 2013
928
415
Lucknow
I have declared the ids for each view as you can see in the layout.xml attached.This NPE is turning out to be quite frustrating now.

do one thing
add a breakpoint to each line
then debug your app if on Eclipse or Android Studio !
and watch the control flow through each line this should narrow you down to the root cause.

Sent from my GT-S5302 using Tapatalk 2
 

Top Liked Posts