5,606,297 Members 32,692 Now Online
XDA Developers Android and Mobile Development Forum

Null Pointer Exception

Tip us?
 
DarkMethod
Old
#1  
Junior Member - OP
Thanks Meter 0
Posts: 3
Join Date: Feb 2014
Default Null Pointer Exception

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
Old
#2  
nikwen's Avatar
Recognized Contributor
Thanks Meter 1241
Posts: 2,624
Join Date: Feb 2013
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(Startingpoint.java:21)
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://forum.xda-developers.com/show....php?t=2325164
 
sak-venom1997
Old
(Last edited by sak-venom1997; 15th February 2014 at 06:06 AM.)
#3  
sak-venom1997's Avatar
Senior Member
Thanks Meter 393
Posts: 901
Join Date: Feb 2013
Location: Lucknow

 
DONATE TO ME
Quote:
Originally Posted by nikwen View Post
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(Startingpoint.java:21)
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://forum.xda-developers.com/show....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

Sent from my GT-S5302 using Tapatalk 2
 
DarkMethod
Old
#4  
Junior Member - OP
Thanks Meter 0
Posts: 3
Join Date: Feb 2014
I am sorry , I should have mentioned it.
Line 21 -> add.setOnClickListener(new View.OnClickListener()
 
sak-venom1997
Old
#5  
sak-venom1997's Avatar
Senior Member
Thanks Meter 393
Posts: 901
Join Date: Feb 2013
Location: Lucknow

 
DONATE TO ME
Quote:
Originally Posted by DarkMethod View Post
I am sorry , I should have mentioned it.
Line 21 -> add.setOnClickListener(new View.OnClickListener()
It means the.view with id bAdd was not found make sure have declared it in your layout

Sent from my GT-S5302 using Tapatalk 2
 
DarkMethod
Old
#6  
Junior Member - OP
Thanks Meter 0
Posts: 3
Join Date: Feb 2014
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.
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


XDA PORTAL POSTS

Speed up the ROM or Kernel Build Process

Creating a kernel, ROM, or any other development projectrequires knowledge and tools. While … more

Google Glass Now Open to Anyone… with $1500 to Spend (Update: Not Anymore)

Google Glass, which recently received its official update … more

Android 4.4.2 OTA Now Available for the Verizon Droid DNA

A couple of days ago, we talked about how the Android 4.4.2 upgrade for the HTC … more