0
votes

I am trying to take the text input from three EditText boxes in my app and store them to a data base. I followed a tutorial to create the data base, but not sure how to see if it works. I do know that when I hit my submit button to post to the data base my app suddenly stops according to android.

public void insertData(int name, int location, int kill){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    db.open();
    cv.put(colDeptID, 1);
    cv.put(colDeptName, "Sales");
    db.insert(deptTable, colDeptID, cv);
    cv.put(colDeptID, 2);
    cv.put(colDeptName, "IT");
    db.insert(deptTable, colDeptID, cv);
    db.close();                   

}

This is the subroutine I call in my subroutine for my button, along with assigning name, location, and kill to new variables within my data base. Here's my button press subroutine:

public void postResults(View button){
    findViewById(R.id.hunters_name);
    findViewById(R.id.location);
    findViewById(R.id.results);
    int kill = R.id.results;
    int location = R.id.location;
    int name = R.id.hunters_name;
    DatabaseHelper dbHelp = new DatabaseHelper(null);
    dbHelp.insertData(name, location, kill);

}

I have no idea why my app is getting killed.

logcat:

FATAL EXCEPTION: main
01-10 21:15:50.568: E/AndroidRuntime(973): java.lang.IllegalStateException: Could not execute method of the activity
01-10 21:15:50.568: E/AndroidRuntime(973):  at android.view.View$1.onClick(View.java:3597)
01-10 21:15:50.568: E/AndroidRuntime(973):  at android.view.View.performClick(View.java:4202)
01-10 21:15:50.568: E/AndroidRuntime(973):  at android.view.View$PerformClick.run(View.java:17340)
01-10 21:15:50.568: E/AndroidRuntime(973):  at android.os.Handler.handleCallback(Handler.java:725)
01-10 21:15:50.568: E/AndroidRuntime(973):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-10 21:15:50.568: E/AndroidRuntime(973):  at android.os.Looper.loop(Looper.java:137)
01-10 21:15:50.568: E/AndroidRuntime(973):  at android.app.ActivityThread.main(ActivityThread.java:5039)
01-10 21:15:50.568: E/AndroidRuntime(973):  at java.lang.reflect.Method.invokeNative(Native Method)
01-10 21:15:50.568: E/AndroidRuntime(973):  at java.lang.reflect.Method.invoke(Method.java:511)
01-10 21:15:50.568: E/AndroidRuntime(973):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-10 21:15:50.568: E/AndroidRuntime(973):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-10 21:15:50.568: E/AndroidRuntime(973):  at dalvik.system.NativeStart.main(Native Method)
01-10 21:15:50.568: E/AndroidRuntime(973): Caused by: java.lang.reflect.InvocationTargetException
01-10 21:15:50.568: E/AndroidRuntime(973):  at java.lang.reflect.Method.invokeNative(Native Method)
01-10 21:15:50.568: E/AndroidRuntime(973):  at java.lang.reflect.Method.invoke(Method.java:511)
01-10 21:15:50.568: E/AndroidRuntime(973):  at android.view.View$1.onClick(View.java:3592)
01-10 21:15:50.568: E/AndroidRuntime(973):  ... 11 more
01-10 21:15:50.568: E/AndroidRuntime(973): Caused by: java.lang.NullPointerException
01-10 21:15:50.568: E/AndroidRuntime(973):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
01-10 21:15:50.568: E/AndroidRuntime(973):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
01-10 21:15:50.568: E/AndroidRuntime(973):  at com.example.test.DatabaseHelper.insertData(DatabaseHelper.java:77)
01-10 21:15:50.568: E/AndroidRuntime(973):  at com.example.test.MainActivity.postResults(MainActivity.java:77)
01-10 21:15:50.568: E/AndroidRuntime(973):  ... 14 more

I see the NullPointerException, but I don't understand how to fix it.

Constructor: public DatabaseHelper(Context context) { super(context, dbName, null,33); }

onCreate: @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub

  db.execSQL("CREATE TABLE "+deptTable+" ("+colDeptID+ " INTEGER PRIMARY KEY , "+
    colDeptName+ " TEXT)");

  db.execSQL("CREATE TABLE "+employeeTable+" ("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
        colName+" TEXT, "+colAge+" Integer, "+colDept+" INTEGER NOT NULL ,FOREIGN KEY ("+colDept+") REFERENCES"+deptTable+" ("+colDeptID+"));");


  db.execSQL("CREATE TRIGGER fk_empdept_deptid " +
    " BEFORE INSERT "+
    " ON "+employeeTable+

    " FOR EACH ROW BEGIN"+
    " SELECT CASE WHEN ((SELECT "+colDeptID+" FROM "+deptTable+"WHERE "+colDeptID+"=new."+colDept+" ) IS NULL)"+
    " THEN RAISE (ABORT,'Foreign Key Violation') END;"+
    "  END;");

  db.execSQL("CREATE VIEW "+viewEmps+
    " AS SELECT "+employeeTable+"."+colID+" AS _id,"+
    " "+employeeTable+"."+colName+","+
    " "+employeeTable+"."+colAge+","+
    " "+deptTable+"."+colDeptName+""+
    " FROM "+employeeTable+" JOIN "+deptTable+
    " ON "+employeeTable+"."+colDept+" ="+deptTable+"."+colDeptID
    );
  //Inserts pre-defined departments
 // InsertDepts(db);  
 }
3
You need to be able to see what Android is telling you about the failure. If you know how to look at the log, do that. If you don't, then finding out how to look at the log needs to be your next step. Android will tell you what the problem is (missing database? missing table/column? etc.) but you need to know how to find it. - prprcupofcoffee

3 Answers

0
votes

Check your Logcat output. You will likely see a NULLPointer exception in there.

$>adb logcat (is how you view your logs).

0
votes

what your method postResult should do? Becasue

findViewById(R.id.hunters_name);
findViewById(R.id.location);
findViewById(R.id.results);

do nothing

and:

int kill = R.id.results;
int location = R.id.location;
int name = R.id.hunters_name;

give you just something like pointer to this views.

If in your xml you have:

 <EditText android:id="@+id/foobar"

Then automatically will be created pointer to this EditText in R.id.

R.id.foobar  

give you just numeric value (identyfier to right resource - your editText)

to get this resource you should use:

View v = findViewById(R.id.foobar);

if it is EditText you can use cast:

EditText et = (EditText) findViewById(R.id.foobar) 

if in this editText you have value and want to read this value:

int kill = et.getText();
0
votes

Probably you need to give context as parameter of DatabaseHelper. Change:

DatabaseHelper dbHelp = new DatabaseHelper(null);

for:

DatabaseHelper dbHelp = new DatabaseHelper(getApplicationContext());

If it won't help, show your constructor, onCreate method and create statement which you use from DatabaseHelper class