1

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

5
  • Which line throws the NullPointerException? Can you please post the error from LogCat? Commented Apr 5, 2014 at 23:14
  • it's the line 'phone1 = (Integer) cv.getAsInteger(Contacts.PHONE);' I will update the post with the logcat error(s) Commented Apr 5, 2014 at 23:16
  • 1
    One definite problem here is that you are declaring a set of new ContentValues(); and then trying to get the value Contacts.PHONE when it is clear that nothing has been added at that point. It's possible that that could be your error. Commented Apr 5, 2014 at 23:20
  • 1
    You are creating a new cv...then you try to geht a value...it's null obviously, as you didn't put anything in before...edit: too slow on phone Commented Apr 5, 2014 at 23:23
  • so instead of doing this in the connector class it would be best to convert to int and increment in another class? Commented Apr 5, 2014 at 23:29

1 Answer 1

2

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});
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I am indeed using my new ContentValues incorrectly. I'm going to keep the field as a string in the database but have the string in the textveiw convert to int, ++, and then save it's contents.

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.