I'm retrieving some data from different Views and then inserting the data into a table. But whenever I press the insert button the application crashes and says "Application stopped responding". I have spent like 2 hours on this and I'm completely stuck here. This is my first application using Sqlite in android.
I have about 10 classes and different variables are coming from different classes so cannot post the complete java code. Here is the java code causing the problem, logcat shows error at the execSQL statement within the if.
insTabVal is an Extra that comes with the Intent and it tells in which Table to put the data, MainActivity is also not null and db is SQLite Database object which is also open. All the Val variables within the SQL query are the values to be inserted in the table and come from different views and they are also not null.
btnInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getValues();
if(b.getString("insTableVal").equals("Donor")){
MainActivity.db.execSQL("INSERT INTO Donor VALUES("+IdVal+","+NameVal+","+AgeVal+","+GenderVal+","+BGVal+");");
}
else if(b.getString("insTableVal").equals("Recipient")){
MainActivity.db.execSQL("INSERT INTO Recipient VALUES("+IdVal+","+NameVal+","+AgeVal+","+GenderVal+","+BGVal+");");
}
}
});
This is the logcat
12-31 06:50:00.038 13562-13562/com.example.bilalrafique.bloodbankmanagementsystem E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bilalrafique.bloodbankmanagementsystem, PID: 13562
java.lang.NullPointerException
at com.example.bilalrafique.bloodbankmanagementsystem.DnRInsertion$1.onClick(DnRInsertion.java:34)
at android.view.View.performClick(View.java:4466)
at android.view.View$PerformClick.run(View.java:18537)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Where is the Null pointer Exception occuring. I can't figure out.
P.S What is a stack trace, and how can I use it to debug my application errors? I have also checked this question but this explains how to look for error through stack trace. I already know that error is at line no 34 and it is null pointer exception. I want to know that how can an Insert statement throw null pointer exception even when all of the variables going in are not null.
Thanks in Advance.