Reputation: 5768
I am trying to update an item in a table. At the moment I have an activity that displays a list of contacts to the user. When they click on the contact I create a new activity which displays a form that allows the user to modify the contacts first and last name.
Every time I attempt to update the table I get a null pointer exception. Below is the error:
java.lang.NullPointerException at com.example.sqlitedemo.EditContact$1.onClick(EditContact.java:41)
This is the code the error seems to be linked to :
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.updateContact(fName.getText().toString(), lName.getText().toString(), id);
Toast.makeText(v.getContext(), "UPDATING", Toast.LENGTH_SHORT).show();
Intent i = new Intent(v.getContext(), MainActivity.class);
startActivity(i);
}
});
this is my updateContact() method in the ContactsDBAdapter class:
public boolean updateContact(String fName, String lName, int rowId){
ContentValues values = new ContentValues();
values.put(COLUMN_FNAME, fName);
values.put(COLUMN_LNAME, lName);
Toast.makeText(context, "Contact updated", Toast.LENGTH_SHORT).show();
return db.update(DB_TABLE, values, COLUMN_ID + "=" + rowId, null) > 0;
}
I pass the data to the edit activity via intent. I retrieve it in the following code. All the data seems to be arriving just fine as I have displayed it in the log.
Intent i = this.getIntent();
fName.setText(i.getStringExtra("fname"));
lName.setText(i.getStringExtra("lname"));
id = i.getIntExtra("_id", 0);
Any ideas?
This is the stack trace:
01-15 02:21:45.053: E/AndroidRuntime(26854): FATAL EXCEPTION: main
01-15 02:21:45.053: E/AndroidRuntime(26854): java.lang.NullPointerException
01-15 02:21:45.053: E/AndroidRuntime(26854): at com.example.sqlitedemo.EditContact$1.onClick(EditContact.java:41)
01-15 02:21:45.053: E/AndroidRuntime(26854): at android.view.View.performClick(View.java:4202)
01-15 02:21:45.053: E/AndroidRuntime(26854): at android.view.View$PerformClick.run(View.java:17340)
01-15 02:21:45.053: E/AndroidRuntime(26854): at android.os.Handler.handleCallback(Handler.java:725)
01-15 02:21:45.053: E/AndroidRuntime(26854): at android.os.Handler.dispatchMessage(Handler.java:92)
01-15 02:21:45.053: E/AndroidRuntime(26854): at android.os.Looper.loop(Looper.java:137)
01-15 02:21:45.053: E/AndroidRuntime(26854): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-15 02:21:45.053: E/AndroidRuntime(26854): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 02:21:45.053: E/AndroidRuntime(26854): at java.lang.reflect.Method.invoke(Method.java:511)
01-15 02:21:45.053: E/AndroidRuntime(26854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-15 02:21:45.053: E/AndroidRuntime(26854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-15 02:21:45.053: E/AndroidRuntime(26854): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 788
Reputation: 5768
I solved the issue myself. It turns out the NPE was being cause because I had failed to create an instance of the Database adapter object and open the connection to the db.
By adding the following code into my onClick listener the problem was fixed:
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter = new ContactsDbAdapter(v.getContext());
adapter.open();
adapter.updateContact(fName.getText().toString(), lName.getText().toString(), id);
Toast.makeText(v.getContext(), "UPDATING", Toast.LENGTH_SHORT).show();
Intent i = new Intent(v.getContext(), MainActivity.class);
startActivity(i);
}
});
Upvotes: 1