简体   繁体   中英

NullPointerException on Intent Extras

I am trying to use Intent to call a new Activity from MainActivity . And I am trying to pass some data through Intent Extras . Below is my code for calling new Activity .

public void tabSelector2(View v) {
    switch (v.getId()) {
        case R.id.veg_tab:
            currentDB = "veg";
            break;
        case R.id.meat_tab :
            currentDB = "meat";
            break;
        default:
            break;
    }

    Intent tabSelect = new Intent(this, TabSelector.class);
    tabSelect.putExtra("db_passed", currentDB);
    startActivity(tabSelect);
}

Inside onCreate for Activity2 I try to use the variable currentDB that I passed. The variable is passed correctly. I printed the value using System.out.println() and logcat produces correct output. But I am getting a NullPointerException when I am trying to use the value. Code snippet for onCreate is below :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTheme(android.R.style.Theme_Holo); // (for Android Built In Theme)

    mydb = new DBHelper(this);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        currentDB = extras.getString("db_passed");
    }

    System.out.println(currentDB);    // Printing here, correct value

    currentList2 = mydb.getAllItemsAsCollection(currentDB);    // Works Perfectly
    itemAdder2.setDbName(currentDB);    // Produces an error :(
    itemAdder2.updateData2(currentList2);

    switch (currentDB) {
        case "veg" :
            setContentView(R.layout.vegetable);
            currentListView = (ListView) findViewById(R.id.veg_list);
            break;
        case "meat" :
            setContentView(R.layout.meat_drawer);
            currentListView = (ListView) findViewById(R.id.meat_list);
            break;
        default:
            break;
    }

    currentListView.setAdapter(itemAdder2);

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

The corresponding setDBName method is as simple as it gets.

public void setDbName(String dbName){
    this.dbName = dbName;
}

I am getting the following error while runtime...

08-02 13:20:23.757    2641-2701/com.example.user.reggaeshark I/OpenGLRenderer? Initialized EGL, version 1.4
08-02 13:20:23.838    2641-2701/com.example.user.reggaeshark D/OpenGLRenderer? Enabling debug mode 0
08-02 13:20:24.150    2641-2701/com.example.user.reggaeshark V/RenderScript? Application requested CPU execution
08-02 13:20:24.163    2641-2701/com.example.user.reggaeshark V/RenderScript? 0xb857f848 Launching thread(s), CPUs 4
08-02 13:20:25.817    2641-2659/com.example.user.reggaeshark W/art? Suspending all threads took: 8.192ms
08-02 13:20:25.843    2641-2641/com.example.user.reggaeshark I/System.out? meat
08-02 13:20:25.861    2641-2641/com.example.user.reggaeshark D/AndroidRuntime? Shutting down VM
08-02 13:20:25.868    2641-2641/com.example.user.reggaeshark E/AndroidRuntime? FATAL EXCEPTION: main
Process: com.example.user.reggaeshark, PID: 2641
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.reggaeshark/com.example.user.reggaeshark.TabSelector}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.user.reggaeshark.ListViewAdapter.setDbName(java.lang.String)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
        at android.app.ActivityThread.access$800(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        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:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.user.reggaeshark.ListViewAdapter.setDbName(java.lang.String)' on a null object reference
        at com.example.user.reggaeshark.TabSelector.onCreate(TabSelector.java:43)
        at android.app.Activity.performCreate(Activity.java:5953)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
        at android.app.ActivityThread.access$800(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        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:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

Please help. Let me know if you guys need anything extra.

Your itemAdder2 variable is not initialized. Attempting to invoke a method on a null reference causes the NPE you're seeing.

Yout adapter object is null - itemAdder2. You should instantiate him like this"

itemAdder2 = new YourAdapter(Some params here);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM