Question

This is a very simple android app. I'm just learning to write code for android now and I am unsure why the line: add.setOnClickListener(new View.OnClickListener() throws a NullPointerException.

public class StartingPoint extends ActionBarActivity{

int counter;
Button add, sub;
TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_starting_point);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
    .add(R.id.container,  new PlaceholderFragment()).commit();
    }

    counter = 0;
    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);
    display = (TextView) findViewById(R.id.tvDisplay);

    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);
        }
    });

}

Here is the XML code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.thenewboston.jaredh.StartingPoint$PlaceholderFragment" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/total"
    android:textSize="40sp" 
    android:id="@+id/tvDisplay"
    />

<Button
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tvDisplay"
    android:layout_below="@+id/tvDisplay"
    android:layout_gravity="center"
    android:text="@string/addButton"
    android:textSize="20sp"
    android:id="@+id/bAdd" />

<Button
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/bAdd"
    android:layout_below="@+id/bAdd"
    android:layout_gravity="center"
    android:text="@string/subButton"
    android:textSize="20sp" 
    android:id="@+id/bSub"/>

Logcat Error:
       03-29 17:44:24.930: D/AndroidRuntime(25367): Shutting down VM
03-29 17:44:24.930: W/dalvikvm(25367): threadid=1: thread exiting with uncaught exception (group=0x41831898)
03-29 17:44:24.940: E/AndroidRuntime(25367): FATAL EXCEPTION: main
03-29 17:44:24.940: E/AndroidRuntime(25367): java.lang.RuntimeException: Unable to start activity ComponentInf{com.thenewboston.jaredh/com.thenewboston.jaredh.StartingPoint}: java.lang.NullPointerException
03-29 17:44:24.940: E/AndroidRuntime(25367):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at android.app.ActivityThread.access$700(ActivityThread.java:165)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at android.os.Looper.loop(Looper.java:137)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at android.app.ActivityThread.main(ActivityThread.java:5455)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at java.lang.reflect.Method.invokeNative(Native Method)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at java.lang.reflect.Method.invoke(Method.java:525)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at dalvik.system.NativeStart.main(Native Method)
03-29 17:44:24.940: E/AndroidRuntime(25367): Caused by: java.lang.NullPointerException
03-29 17:44:24.940: E/AndroidRuntime(25367):    at com.thenewboston.jaredh.StartingPoint.onCreate(StartingPoint.java:39)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at android.app.Activity.performCreate(Activity.java:5372)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
03-29 17:44:24.940: E/AndroidRuntime(25367):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
03-29 17:44:24.940: E/AndroidRuntime(25367):    ... 11 more

As you can see it is a very simple app just adding 1 to the text field when either button is clicked. Any help could be greatly apprecitated. Just unsure why the NullPointerException is being caused.

Was it helpful?

Solution

I would guess that the XML you presented (without saying which file it really is, and despite that the XML text is not even valid) is the layout of the PlaceholderFragment you add at the beginning. However, the fragment is not immediately added, just scheduled to be added by the FragmentManager. Therefore when you are immediately trying to find its UI components, the activity fails to do so, and the add reference is set to null by findViewById.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top