Reputation: 118
I am currently working from a project used in a tutorial that displays contacts in a listview. All the fields in the database are used and saved as strings. A custom adapter class is being used for the edit and delete buttons - the buttons appear on the listview, per item. Clicking on edit leads to a separate activity to in which details for that record are parsed in.
I have added another button that I'd like to update a record when clicked and reload the activity. When you press the button it calls a method from the connector
method:
public void updateContact(long id, String name, String phone, String mail,
String fb, byte[] blob) {
ContentValues cv = new ContentValues();
cv.put(Contacts.NAME, name);
cv.put(Contacts.PHONE, phone);
cv.put(Contacts.MAIL, mail);
cv.put(Contacts.FB, fb);
cv.put(Contacts.IMAGE, blob);
db = sqlHp.getWritableDatabase();
db.update(Contacts.TABLE, cv, Contacts.ID + "=" + id, null);
db.close();
}
I would like phone
to convert the string to an integer, increment the value by 1 and then save using contentvalues as a string. Here is what I've tried:
public void updateContact(long id, String name, String phone,
String mail, String fb, byte[] blob) {
ContentValues cv = new ContentValues();
cv.put(Contacts.NAME, name);
int phone1 = 0;
phone1 = (Integer) cv.getAsInteger(Contacts.PHONE);
phone1++;
phone = String.valueOf(phone1);
cv.put(Contacts.PHONE, phone);
cv.put(Contacts.MAIL, mail);
cv.put(Contacts.FB, fb);
cv.put(Contacts.IMAGE, blob);
db = sqlHp.getWritableDatabase();
db.update(Contacts.TABLE, cv, Contacts.ID + "=" + id, null);
db.close();
}
This gives me a null pointer exception
error. I'm not sure where to go from here.
This is the method from the custom adapter which holds the onclick method
setPresent = (ImageButton) v.findViewById(R.id.setPresent);
setPresent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sqlCon.updateContact(id, name, phone, email, fb, image);
Intent intent = new Intent(context, MyContactsActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
It's when I click this button the null pointer exception error occurs. These are the lines it flags up:
sqlCon.updateContact(id, name, phone, email, fb, image);
- from the customadapter
and
phone1 = (Integer) cv.getAsInteger(Contacts.PHONE);
- from the connector
Upvotes: 1
Views: 1912
Reputation: 124844
This part doesn't really make sense in your code:
ContentValues cv = new ContentValues();
int phone1 = (Integer) cv.getAsInteger(Contacts.PHONE);
You are trying to get Contacts.PHONE
from a ContentValues
you just created.
Perhaps you wanted to do something like this instead:
cv.put(Contacts.PHONE, Integer.parseInt(phone) + 1);
If you clean up this part, the NullPointerException
will probably disappear naturally.
Also, it's safer to update tables using ?
placeholders for parameters instead of puttin them in the query string, like this:
db.update(Contacts.TABLE, cv, Contacts.ID + " = ?", new long[]{id});
Upvotes: 2