0

Alright I'm back once again trying to get some help with my contacts part of this app I am building. Basically I am trying to select a contact from the in system contact list. This part is done and working just fine. - Where I am having issue is when you pick a name its supposed to set that persons name and phone number into their own respective text views within my app. Logcat is throwing a Null pointer exception supposedly on a specific line of my code. So here is the chunk I am working with:

    public void onActivityResult(int reqCode, int resultCode, Intent data) {

    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
        case (1) :
            if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
            cursor.moveToFirst();
            String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
            //String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.NUMBER));
            name1.setText(name);
            //num1.setText(number);
            }
            break;
    }
}

Supposedly the issue (NPE) lies on line 103 of my code which is the following:

name1.setText(name);

I commented out the number portion of the code because I figure if I can get this part the next part will fall in line. If anyone see's any issues with that feel free to tell me.

Aside of that - thanks for the help in advance.

CodeMonkeyAlx
  • 813
  • 4
  • 16
  • 32
  • 1
    name1 is obviously null. Where are you initializing it? It isn't in the code you posted. – Gabe Sechan Jun 26 '14 at 23:06
  • @GabeSechan Silly me, you are right I looked in the code and I had two num1's - The only other issue I am having is that the number is causing a crash its telling me that "NUMBER" does not exits in the column's - Any ideas? – CodeMonkeyAlx Jun 27 '14 at 01:25

1 Answers1

1

you probably have not initialized name1 as @Gabe mentioned (in on create or on resume you should be saying name1 = (TextView)findViewById(R.id.[yourTextViewID goes here])

if thats not the case have you truied logging 'name' to see if it is not null?

erik
  • 4,946
  • 13
  • 70
  • 120
  • You are both correct - Thank you I only have one other issue right now and that is that logcat is telling me "NUMBER" does not exist in the columns. – CodeMonkeyAlx Jun 27 '14 at 01:26