Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5958519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:33:17+00:00 2026-05-22T18:33:17+00:00

I hope to get some directions on how to solve the problems on NullPointerException

  • 0

I hope to get some directions on how to solve the problems on NullPointerException when passing intents. I wanted to create a list view which I can click further in to display some associated information. So basically the main list is able to run but when I click on the entry, it just crashes.

I’ve checked LogCat and it says I’m having Nullpointerexception , I’m unfamiliar with how to check/look out for Null variables. I think my mRowId is null but I don’t know how to make it a defined value.

Can anyone help me spot a problem in the intent passing below ?

The main file passing the intent is this.

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Intent i = new Intent(this, drugdetail.class);  
    i.putExtra(DrugsDbAdapter.KEY_ROWID, id);

    startActivity(i);
}

The second file reading the intent is this.

package com.paad.medboxsd;

public class drugdetail extends Activity{

private TextView mDrugText;
private TextView mContentText;

private Long mRowId;

    DrugsDbAdapter.DatabaseHelper mDbHelper = new DrugsDbAdapter.DatabaseHelper(this);

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

        mDrugText = (TextView) findViewById(R.id.drug);
        mContentText = (TextView) findViewById(R.id.content);

        //potentially where the problem on Nullpointers lie where LogCat    says null pointer exception from fetchDrugs(). Meaning likely that mRowId is Null.
        //mRowId = savedInstanceState != null ? savedInstanceState.getLong(DrugsDbAdapter.KEY_ROWID) : null;

        Bundle extras = getIntent().getExtras();

        mRowId = extras.getLong(DrugsDbAdapter.KEY_ROWID);

        Cursor drug = mDbHelper.fetchDrug(mRowId);

        //Managing Cursor to pluck values to display
        startManagingCursor(drug);

        mDrugText.setText(drug.getString(drug.getColumnIndexOrThrow(DrugsDbAdapter.KEY_DRUG)));

        mContentText.setText(drug.getString(drug.getColumnIndexOrThrow(DrugsDbAdapter.KEY_CONTENT)));

    }

The LogCat output is :

05-26 15:27:48.698: ERROR/AndroidRuntime(4829): FATAL EXCEPTION: main
05-26 15:27:48.698: ERROR/AndroidRuntime(4829): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.medboxsd/com.paad.medboxsd.drugdetail}: java.lang.NullPointerException
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.os.Looper.loop(Looper.java:123)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at java.lang.reflect.Method.invoke(Method.java:521)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at dalvik.system.NativeStart.main(Native Method)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829): Caused by: java.lang.NullPointerException
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at com.paad.medboxsd.DrugsDbAdapter$DatabaseHelper.fetchDrug(DrugsDbAdapter.java:195)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at com.paad.medboxsd.drugdetail.onCreate(drugdetail.java:35)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-26 15:27:48.698: ERROR/AndroidRuntime(4829):     ... 11 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:33:17+00:00Added an answer on May 22, 2026 at 6:33 pm

    You set mRowId here

    mRowId = savedInstanceState != null ? savedInstanceState.getLong(DrugsDbAdapter.KEY_ROWID) : null;
    

    but then you immediately set it again here

        Bundle extras = getIntent().getExtras();
        mRowId = extras.getLong(DrugsDbAdapter.KEY_ROWID);
    

    and use it without checking if it’s null or not.


    Do this:

    mRowId = (savedInstanceState != null) ?
        savedInstanceState.getLong(DrugsDbAdapter.KEY_ROWID) : null;
    if (mRowId == null)
    {
      Bundle extras = getIntent().getExtras();
      mRowId = (extras != null) ? extras.getLong(DrugsDbAdapter.KEY_ROWID) : null;
    }
    
    if (mRowId != null)
    {
       //database stuff here
    }
    else
    {
       //error handling here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.