Android 5.0.2 and New Factory Images For Nexus 7 (Wi-Fi) Available

Android 5.0 is has been publicly available since early November, and the … more

Github Tutorial App Shows You How to Use… Github

Github can get pretty complicated and using it can become quite daunting, especially … more

Google Glass-like Clip-On For Regular Glasses Developed by Sony

For the price of $2,000, a pair of glasses that can run apps, take pictures, … more

Micromax Takes OnePlus to Court! Android Wear Receives Lollipop – XDA TV

Android 5.0 Lollipop is available officially for the Moto 360! … more

Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

Null Pointer Exception

OP DarkMethod

14th February 2014, 03:08 PM   |  #1  
OP Junior Member
Thanks Meter: 0
 
3 posts
Join Date:Joined: Feb 2014
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.
14th February 2014, 09:53 PM   |  #2  
nikwen's Avatar
Recognized Contributor
Thanks Meter: 1,408
 
2,805 posts
Join Date:Joined: 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
15th February 2014, 07:03 AM   |  #3  
sak-venom1997's Avatar
Senior Member
Flag Lucknow
Thanks Meter: 409
 
921 posts
Join Date:Joined: Feb 2013
Donate to Me
More
Quote:
Originally Posted by nikwen

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
Last edited by sak-venom1997; 15th February 2014 at 07:06 AM.
15th February 2014, 07:50 AM   |  #4  
OP Junior Member
Thanks Meter: 0
 
3 posts
Join Date:Joined: Feb 2014
I am sorry , I should have mentioned it.
Line 21 -> add.setOnClickListener(new View.OnClickListener()
15th February 2014, 07:59 AM   |  #5  
sak-venom1997's Avatar
Senior Member
Flag Lucknow
Thanks Meter: 409
 
921 posts
Join Date:Joined: Feb 2013
Donate to Me
More
Quote:
Originally Posted by DarkMethod

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
17th February 2014, 03:23 PM   |  #6  
OP Junior Member
Thanks Meter: 0
 
3 posts
Join Date:Joined: 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.
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes