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

时间:2013-08-13 12:41:06

标签: android sqlite insert

我通过执行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)

有人能在这看到问题吗?

提前致谢。

克里斯

2 个答案:

答案 0 :(得分:1)

这样做

value == "Retrieve"

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

value.equals("Retrieve")

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

答案 1 :(得分:0)

您的SQLite数据库可能会导致问题。对于openOrCreateDatabase,如果您要使用此方法,则需要提供实际的path to the database。现在你只是提供看起来像文件名的东西。