0

INSERT ステートメントを実行すると、NULL ポインター例外が発生し続けます。

私がやろうとしているのは、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 が null になります。

于 2013-08-13T12:50:46.523 に答える
0

データベースSQLiteが問題を引き起こしている可能性があります。この方法を使用する場合は、データベースへopenOrCreateDatabaseの実際のパスを指定する必要があります。現在、ファイルの名前のように見えるものを提供しているだけです。

于 2013-08-13T12:53:40.877 に答える