Android intents returning Nullpointerexception
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
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
}
精彩评论