gpt4 book ai didi

android - 当我尝试从数据库表 android 中获取不同的值时出现致命异常

转载 作者:行者123 更新时间:2023-11-29 21:54:23 25 4
gpt4 key购买 nike

我在数据库中有一个名为“EMR_PT_REFERNCE”的表,其中包含患者引用详细信息...表中有一个名为“EPR_OUT_HOSPITAL”的列,其中包含外部医院的名称...我试图从表中获取“EPR_OUT_HOSPITAL”的不同值...如果所有行都填满了值,那么它工作正常..但是如果任何行为空,则会触发异常并且我的应用程序强行关闭...请帮忙

---------------------------------这是代码-------- ------------------------------

  // 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): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)11-29 15:26:38.582: E/AndroidRuntime (18519): 在 android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:1981)11-29 15:26:38.582: E/AndroidRuntime(18519): 在 android.app.ActivityThread.access$600(ActivityThread.java:123)11-29 15:26:38.582: E/AndroidRuntime(18519): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)11-29 15:26:38.582: E/AndroidRuntime (18519): 在 android.os.Handler.dispatchMessage (Handler.java:99)11-29 15:26:38.582: E/AndroidRuntime (18519): 在 android.os.Looper.loop (Looper.java:137) 11-29 15:26:38.582: E/AndroidRuntime(18519): 在 android.app.ActivityThread.main(ActivityThread.java:4424)11-29 15:26:38.582:E/AndroidRuntime(18519):在 java.lang.reflect.Method.invokeNative( native 方法)11-29 15:26:38.582:E/AndroidRuntime(18519):在 java.lang.reflect.Method.invoke(Method.java:511)11-29 15:26:38.582:E/AndroidRuntime(18519):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)11-29 15:26:38.582: E/AndroidRuntime (18519): 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:551)11-29 15:26:38.582:E/AndroidRuntime(18519):在 dalvik.system.NativeStart.main( native 方法)11-29 15:26:38.582: E/AndroidRuntime(18519): 由 java.lang.NullPointerException 引起11-29 15:26:38.582: E/AndroidRuntime(18519): 在 android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)11-29 15:26:38.582:E/AndroidRuntime(18519):在 com.stpl.emr.database.DataBaseHelper.getDatabasePath(DataBaseHelper.java:241)11-29 15:26:38.582:E/AndroidRuntime(18519):在 com.stpl.emr.database.DataBaseHelper.checkDatabase(DataBaseHelper.java:204)11-29 15:26:38.582:E/AndroidRuntime(18519):在 com.stpl.emr.database.DataBaseHelper.initialize(DataBaseHelper.java:61)11-29 15:26:38.582:E/AndroidRuntime(18519):在 com.stpl.emr.database.DataBaseHelper.getInstance(DataBaseHelper.java:93)11-29 15:26:38.582:E/AndroidRuntime(18519):在 com.stpl.emr.patientlist.PatientList。(PatientList.java:45)11-29 15:26:38.582:E/AndroidRuntime(18519):在 java.lang.Class.newInstanceImpl( native 方法)11-29 15:26:38.582: E/AndroidRuntime(18519): 在 java.lang.Class.newInstance(Class.java:1319)11-29 15:26:38.582: E/AndroidRuntime (18519): 在 android.app.Instrumentation.newActivity (Instrumentation.java:1023)11-29 15:26:38.582:E/AndroidRuntime(18519):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)11-29 15:26:38.582: E/AndroidRuntime(18519): ... 11 更多

最佳答案

试试看:

改变

  do {
hospitalNameListArray.add(c1.getString(c1.getColumnIndex("EPR_OUT_HOSPITAL")));
} while (c1.moveToNext());
}

  do {
if (null!=c1.getString(c1.getColumnIndex("EPR_OUT_HOSPITAL")))
hospitalNameListArray.add(c1.getString(c1.getColumnIndex("EPR_OUT_HOSPITAL")));
} while (c1.moveToNext());
}

关于android - 当我尝试从数据库表 android 中获取不同的值时出现致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13623436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com