0

So I'm getting a Null Pointer error on line 33 of my project but I can't tell what it is. Any help would be much appreciated.

    06-15 21:15:24.813: E/AndroidRuntime(15696): java.lang.RuntimeException: Unable to start activiy
ComponentInfo{yc.android.yourchallenger/yc.android.yourchallenger.MainActivity}: java.lang.NullPointerException
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.ActivityThread.access$700(ActivityThread.java:168)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.os.Looper.loop(Looper.java:137)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.ActivityThread.main(ActivityThread.java:5493)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at java.lang.reflect.Method.invokeNative(Native Method)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at java.lang.reflect.Method.invoke(Method.java:525)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at dalvik.system.NativeStart.main(Native Method)
06-15 21:15:24.813: E/AndroidRuntime(15696): Caused by: java.lang.NullPointerException
06-15 21:15:24.813: E/AndroidRuntime(15696):    at yc.android.yourchallenger.MainActivity.onCreate(MainActivity.java:33)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.Activity.performCreate(Activity.java:5372)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
06-15 21:15:24.813: E/AndroidRuntime(15696):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
06-15 21:15:24.813: E/AndroidRuntime(15696):    ... 11 more

Here is the MainActivity where I am getting the error package yc.android.yourchallenger;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

private String [] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;    
private CharSequence mDrawerTitle;
private CharSequence mTitle;

 public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.nav_drawer, mNavigationDrawerItemTitles));
        // Set the list's click listener
        mDrawerList.setOnItemClickListener((OnItemClickListener) new DrawerItemClickListener());
}

 private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);           
        }           
    }

 private void selectItem(int position) {
     Fragment fragment = new CreateFragment();
     Bundle args = new Bundle();
     args.putInt("key", position);
     fragment.setArguments(args);

     FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.content_frame, fragment)
                       .commit();
     // Highlight the selected item, update the title, and close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mNavigationDrawerItemTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
 }   

 @Override
 public void setTitle(CharSequence title){
     mTitle = title;
     getActionBar().setTitle(mTitle);
 }


} 
5
  • it looks like mDrawerList is null in the line mDrawerList.setOnItemClickListener((OnItemClickListener) new DrawerItemClickListener()); Commented Jun 16, 2014 at 2:22
  • @Zusee Weeking Line 33 is mDrawerList.setAdapter(new ArrayAdapter<String>(this, Commented Jun 16, 2014 at 2:41
  • Make sure that you have ListView called left_drawer inside your activity_main layout. Check spellings carefully. Commented Jun 16, 2014 at 2:43
  • I try to create my drawer view in an xml file called drawer, so I have <android.support.v4.widget.DrawerLayout... in there, and I do have the left_drawer, but should i switch that to my activity_main.xml? Commented Jun 16, 2014 at 2:48
  • Try the answer I suggested below and see.. Commented Jun 16, 2014 at 2:51

2 Answers 2

1

Make sure your activity's layout contains a ListView with the android:id="@+id/left_drawer", otherwise the call to findViewById() will return null.

Sign up to request clarification or add additional context in comments.

3 Comments

I do have a file called nav_drawer.xml that contains that has android:id="@+id/left_drawer
But your Activity's layout is activity_main.xml according to the code you posted. You have to have the LIstView with this id in the layout your activity is using
Awesome. It works now. I don't have the reputation to +1, but I will when I do.
0

As I suggested in the comment, make sure that you have ListView called left_drawer inside your activity_main layout. Otherwise you can't call it using findViewById().

You did not add any values to your mNavigationDrawerItemTitles string array and it is added to the adapter. Also called setOnItemClickListener(). But there are not values in the list. Because your mNavigationDrawerItemTitles is empty. Add some values to the string array and test.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.