0

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

任何人都可以在这里看到问题吗?

提前致谢。

克里斯

4

2 回答 2

1

这样做

value == "Retrieve"

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

value.equals("Retrieve")

否则匹配失败并且 sampleDB 为空。

于 2013-08-13T12:50:46.523 回答
0

Your SQLite database is likley causing the problem. For openOrCreateDatabase you need to supply the actual path to the database if you're going to use this method. Right now you're just supplying what looks like the name of the file.

于 2013-08-13T12:53:40.877 回答