当我试图从数据库表android获取不同的值时,致命异常

时间:2012-11-29 10:14:54

标签: android

我在数据库中有一个名为'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):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):引起: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):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):在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 more

1 个答案:

答案 0 :(得分:2)

试试:

更改

  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());
}