i have a table in the database named ‘EMR_PT_REFERNCE’ which contains patient reference details… there is a column named ‘EPR_OUT_HOSPITAL’ in the table which contains the names of the outside hospitals…
i tried to get distinct values of ‘EPR_OUT_HOSPITAL’ from the table…if all the rows are filled up with values, then its working fine.. but if any of the row is empty then it triggers the exception and my application closed forcely… please help
———————————–here is the code————————————–
// to load out hospital/ clinic data into spinner
private void loadOutHospitalListSpinner() {
// TODO Auto-generated method stub
Log.v("this is inside load out hospital list spinner------------>","");
try {
if (getFirstRun()) {
sampleDB = dbAdapter.getDatabase();
setRunned();
} else {
sampleDB = dbAdapter.getWritableDatabase();
}
Cursor c1 = sampleDB.rawQuery("select DISTINCT EPR_OUT_HOSPITAL from EMR_PT_REFERNCE",null);
if (c1 != null && c1.getCount() != 0) {
hospitalNameListArray.clear();
if (c1.moveToFirst()) {
do {
hospitalNameListArray.add(c1.getString(c1.getColumnIndex("EPR_OUT_HOSPITAL")));
} while (c1.moveToNext());
}
}
c1.close();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, hospitalNameListArray);
// dropdownlist
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnOutHospitalList.setAdapter(dataAdapter);
} catch (Exception e) {
// TODO: handle exception
System.out.println("CAT LIST ERROR IS: " + e.getMessage());
}
}
-----------------------------------here is the Logcat Error--------------------------------
11-29 15:26:38.572: D/AndroidRuntime(18519): Shutting down VM
11-29 15:26:38.572: W/dalvikvm(18519): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-29 15:26:38.582: E/AndroidRuntime(18519): FATAL EXCEPTION: main
11-29 15:26:38.582: E/AndroidRuntime(18519): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.stpl.emr.login/com.stpl.emr.patientlist.PatientList}: java.lang.NullPointerException
11-29 15:26:38.582: E/AndroidRuntime(18519): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
11-29 15:26:38.582: E/AndroidRuntime(18519): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-29 15:26:38.582: E/AndroidRuntime(18519): at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-29 15:26:38.582: E/AndroidRuntime(18519): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-29 15:26:38.582: E/AndroidRuntime(18519): at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 15:26:38.582: E/AndroidRuntime(18519): at android.os.Looper.loop(Looper.java:137)
11-29 15:26:38.582: E/AndroidRuntime(18519): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-29 15:26:38.582: E/AndroidRuntime(18519): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 15:26:38.582: E/AndroidRuntime(18519): at java.lang.reflect.Method.invoke(Method.java:511)
11-29 15:26:38.582: E/AndroidRuntime(18519): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-29 15:26:38.582: E/AndroidRuntime(18519): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-29 15:26:38.582: E/AndroidRuntime(18519): at dalvik.system.NativeStart.main(Native Method)
11-29 15:26:38.582: E/AndroidRuntime(18519): Caused by: java.lang.NullPointerException
11-29 15:26:38.582: E/AndroidRuntime(18519): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
11-29 15:26:38.582: E/AndroidRuntime(18519): at com.stpl.emr.database.DataBaseHelper.getDatabasePath(DataBaseHelper.java:241)
11-29 15:26:38.582: E/AndroidRuntime(18519): at com.stpl.emr.database.DataBaseHelper.checkDatabase(DataBaseHelper.java:204)
11-29 15:26:38.582: E/AndroidRuntime(18519): at com.stpl.emr.database.DataBaseHelper.initialize(DataBaseHelper.java:61)
11-29 15:26:38.582: E/AndroidRuntime(18519): at com.stpl.emr.database.DataBaseHelper.getInstance(DataBaseHelper.java:93)
11-29 15:26:38.582: E/AndroidRuntime(18519): at com.stpl.emr.patientlist.PatientList.(PatientList.java:45)
11-29 15:26:38.582: E/AndroidRuntime(18519): at java.lang.Class.newInstanceImpl(Native Method)
11-29 15:26:38.582: E/AndroidRuntime(18519): at java.lang.Class.newInstance(Class.java:1319)
11-29 15:26:38.582: E/AndroidRuntime(18519): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
11-29 15:26:38.582: E/AndroidRuntime(18519): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
11-29 15:26:38.582: E/AndroidRuntime(18519): … 11 more
try that:
Change
to