title: Getting -1 with getColumnIndex() while retrieving a contact number - Android
body: <p>I'm trying to retrieve a contact's number. I can access the contact name with no problems but when I try to access the contact number I get an exception because <code>getColumnIndex</code> returns -1, which means that it couldn't find the column I requested. But I can't understand why it does not find the column when from the Android docs I see that the column does exist.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public ArrayList&lt;String&gt; getContactById(String contactId)
      {
          Log.d("XYZ", "Contact id -&gt; " + contactId);
          ArrayList&lt;String&gt; contact = new ArrayList&lt;String&gt;();
          ContentResolver cr = context.getContentResolver();
  final String[] projection = new String[] {
                  Contacts.DISPLAY_NAME,  // the name of the contact
                  Contacts.PHOTO_ID       // the id of the column in the data table for the image
              };
  
          final Cursor curContact = cr.query(
                  Contacts.CONTENT_URI,
                  projection,
                  Contacts._ID + "=?",    // filter entries on the basis of the contact id
                  new String[]{String.valueOf(contactId)},    // the parameter to which the contact id column is compared to
                  null);
  
          if(curContact.moveToFirst()) {
              final String name = curContact.getString(curContact.getColumnIndex(Contacts.DISPLAY_NAME));
              final String photoId = curContact.getString(curContact.getColumnIndex(Contacts.PHOTO_ID));
                  Log.d("XYZ", "Name -&gt; " + name);
                  Log.d("XYZ", "Photo id -&gt; " + photoId);
              }
  
  
  
          // Get the phone numbers
  
          final String[] phoneNumbersProjection = new String[] {
                  Phone.NUMBER,
                  Phone.TYPE,
          };
  
          final Cursor curPhone = cr.query(
                  Phone.CONTENT_URI,
                  projection,
                  Data.CONTACT_ID + " = " + contactId, null,
                  //new String[]{String.valueOf(contactId)},
                  null);
  
          if(curPhone.moveToFirst()) {
              final int contactNumberColumnIndex = curPhone.getColumnIndex(Phone.NUMBER);
              final int contactTypeColumnIndex = curPhone.getColumnIndex(Phone.TYPE);
  
              Log.d("XYZ", "contactNumberColumnIndex -&gt; " + contactNumberColumnIndex);
              Log.d("XYZ", "contactTypeColumnIndex -&gt; " + contactTypeColumnIndex);
  
              while(!curPhone.isAfterLast()) {
                  final String number = curPhone.getString(contactNumberColumnIndex);
                  final int type = curPhone.getInt(contactTypeColumnIndex);
                  final int typeLabelResource = Phone.getTypeLabelResource(type);
                  Log.d("XYZ", "Contact number -&gt; " + number);
                  //Log.d("XYZ", "Number Type -&gt; " + typeLabelResource);
                  //doSomethingWithAContactPhoneNumber(number, typeLabelResource);
                  curPhone.moveToNext();
              }
  
          }
          curPhone.close();
          curContact.close();
          return contact;
      }
  </code></pre>
  
  <p>With: </p>
  
  <pre><code>Log.d("XYZ", "contactNumberColumnIndex -&gt; " + contactNumberColumnIndex);
  Log.d("XYZ", "contactTypeColumnIndex -&gt; " + contactTypeColumnIndex);
  </code></pre>
  
  <p>I receive:</p>
  
  <pre><code>contactNumberColumnIndex -&gt; -1
  contactTypeColumnIndex -&gt; -1
  </code></pre>
  
  <p>Can anyone figure out what is wrong with my code?</p>
  
  <p>Thanks!</p>