0

I keep getting a null pointer exception via the execution of my INSERT statement.

All I am trying to do is insert a new row into the tbl_building table.

Here is the code:

Declaring the database:

SQLiteDatabase sampleDB;

To retreive context for the database:

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

To insert data into the table:

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

My stack trace:

    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)

Can anyone see the problem here?

Thanks in advance.

Chris

1
  • yes, i'll test that now Commented Aug 13, 2013 at 12:49

2 Answers 2

1

Doing this

value == "Retrieve"

compares them as objects. To compare as strings use

value.equals("Retrieve")

otherwise both matches fail and sampleDB is null.

Sign up to request clarification or add additional context in comments.

1 Comment

you pointed me to the right direction, I was just simply calling the wrong method to get the 'value' value. Thanks for your time.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.