Regain Double Tap to Wake Functionality on the Nexus 6

A few months ago, Google announced its newest flagship device, the Nexus 6, alongside … more

Clean Your Recent Apps – XDA Xposed Tuesday

In this day and age, you have quite a few apps installed and running on your Android … more

Android Studio Reaches Release Candidate Status

Android app developers have a few options when comes to Android IDEs. One of the most … more

Connect ALL THE THINGS with Xender

Smartphones, with all of their all connectivity options courtesy of OEMs, can easily connect themselves … 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,397
 
2,785 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: 408
 
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: 408
 
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