Keith Keith - 4 months ago 29
Java Question

NullPointerException: on NavigationDrawerFragment.setUp()

I'm getting this error and I don't know why. This same code was working before; the problem started occuring after an update at some point.

Stack trace:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.domain.appname, PID: 17964
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.domain.appname/com.domain.appname.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.domain.appname.NavigationDrawerFragment.setUp(int, android.support.v4.widget.DrawerLayout)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5431)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.domain.appname.NavigationDrawerFragment.setUp(int, android.support.v4.widget.DrawerLayout)' on a null object reference
at com.domain.appname.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6056)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441) 
at android.app.ActivityThread.access$800(ActivityThread.java:162) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5431) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)


Here's the offending code in MainActivity.java:

...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (BuildConfig.DEBUG){ Log.d(TAG, "Created in debug mode"); }

NavigationDrawerFragment mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

// Set up the drawer.
Log.d(TAG, "Setting up drawer");
mNavigationDrawerFragment.setUp( // Line 40
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout)
);

...


I've found two questions that seem to have similar problems. (Here's the first and the second) The first doesn't seem relevant, and the second is unanswered.

I'm baffled.
drawer_layout
and
navigation_drawer
do not throw "symbol unresolved" errors. Like I said, this all used to work...

Answer

You are never calling Activity.setContentView(int). This is how it should be:

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

    // Rest.
}