.insert() & .execSQL() 导致 Android 中的 NULL 指针异常



通过执行INSERT语句,我一直得到一个空指针异常。

我所要做的就是在tbl_building表中插入一个新行。

这是代码:

声明数据库:

SQLiteDatabase sampleDB;

检索数据库的上下文:

if(value == "Retrieve"){
    sampleDB = getBaseContext().openOrCreateDatabase(ClientList.retrievedClient+".db", MODE_PRIVATE, null);
} 
else if (value == "Create"){
    sampleDB = getBaseContext().openOrCreateDatabase(CreateClient.createdClient+".db", MODE_PRIVATE, null);
}

将数据插入表格:

            templateSelected = 0;
            try {
                templateSelected = Integer.parseInt(selectedTemplate.toString());
                Log.e("Int : ", selectedTemplate);
            } 
            catch(NumberFormatException nfe) {
                String error = nfe.toString();
                Log.e("Could not parse: ", error);
            }
            for(int i = 0; i < templateSelected; i++){
                int buildingName = i+1;
                String buildingNameTwo = "B"+buildingName;
                String buildingDesc = "Template Description";
                String roofType = "Flat - Insulated";
                String roofPitchDepth = "5"; 
                String wallType = "Cavity - Insulated";
                String coolingType = "Natural";
                String wattage = "Wattage";
                String radio = "Radio";
                ContentValues args = new ContentValues();
                args.put("buildingName", buildingNameTwo);
                args.put("buildingDesc", buildingDesc);
                args.put("roofType", roofType);
                args.put("roofPitchDepth", roofPitchDepth);
                args.put("wallType", wallType);
                args.put("coolingType", coolingType);
                args.put("localCoolingWattage", wattage);
                args.put("localCoolingControls", radio);
                Log.e("Building Name", buildingNameTwo);
                Log.e("Building Description", buildingDesc);
                Log.e("Roof Type", roofType);
                Log.e("Roof Pitch Depth", roofPitchDepth);
                Log.e("Wall Type", wallType);
                Log.e("Cooling Type", coolingType);
                Log.e("Wattage", wattage);
                Log.e("Radio", radio);
                //sampleDB.insert("tbl_building", null, args);
                try{
                    sampleDB.execSQL("INSERT INTO tbl_building (b_id, buildingName, buildingDesc, roofType, roofPitchDepth, wallType, coolingType, localCoolingWattage, localCoolingControls) Values ('123','"+buildingNameTwo+"','"+buildingDesc+"','"+roofType+"','"+roofPitchDepth+"','"+wallType+"','"+coolingType+"','"+wattage+"','"+radio+"')"); 
                }
                catch(){
                }
            }
            sampleDB.close();

我的堆栈跟踪:

    08-13 13:34:46.280: E/Building Name(4956): B1
08-13 13:34:46.280: E/Building Description(4956): Template Description
08-13 13:34:46.280: E/Roof Type(4956): Flat - Insulated
08-13 13:34:46.280: E/Roof Pitch Depth(4956): 5
08-13 13:34:46.280: E/Wall Type(4956): Cavity - Insulated
08-13 13:34:46.280: E/Cooling Type(4956): Natural
08-13 13:34:46.280: E/Wattage(4956): Wattage
08-13 13:34:46.284: E/Radio(4956): Radio
08-13 13:34:46.292: E/AndroidRuntime(4956): FATAL EXCEPTION: main
08-13 13:34:46.292: E/AndroidRuntime(4956): java.lang.NullPointerException
08-13 13:34:46.292: E/AndroidRuntime(4956):     at com.android.sec.BuildingTemplateList$2$1.onClick(BuildingTemplateList.java:125)
08-13 13:34:46.292: E/AndroidRuntime(4956):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
08-13 13:34:46.292: E/AndroidRuntime(4956):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 13:34:46.292: E/AndroidRuntime(4956):     at android.os.Looper.loop(Looper.java:123)
08-13 13:34:46.292: E/AndroidRuntime(4956):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-13 13:34:46.292: E/AndroidRuntime(4956):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 13:34:46.292: E/AndroidRuntime(4956):     at java.lang.reflect.Method.invoke(Method.java:521)
08-13 13:34:46.292: E/AndroidRuntime(4956):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
08-13 13:34:46.292: E/AndroidRuntime(4956):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
08-13 13:34:46.292: E/AndroidRuntime(4956):     at dalvik.system.NativeStart.main(Native Method)

有人能看到这里的问题吗?

提前谢谢。

Chris

执行此

value == "Retrieve"

将它们作为对象进行比较。使用作为字符串进行比较

value.equals("Retrieve")

否则两个匹配都失败,sampleDB为null。

您的SQLite数据库可能会导致此问题。对于openOrCreateDatabase,如果要使用此方法,则需要提供数据库的实际路径。现在,您只需要提供看起来像文件名的内容。

最新更新