title: How to get contact name when receiving SMS
body: <p>I have the following code to receive an SMS and I am trying to get the contact name.</p>
  
  <pre><code>package com.example.smsTest;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.database.Cursor;
  import android.net.Uri;
  import android.os.Bundle;
  import android.provider.ContactsContract;
  import android.provider.ContactsContract.PhoneLookup;
  import android.telephony.SmsMessage;
  import android.widget.Toast;
  
  public class SmsReceiver extends BroadcastReceiver {
      private SQLiteAdapter mySQLiteAdapter;
  
      @Override
      public void onReceive(Context context, Intent intent) {
          mySQLiteAdapter = new SQLiteAdapter(context);
          mySQLiteAdapter.openToRead();
  
          Message message = null;
  
          Bundle extras = intent.getExtras();
          if (extras == null)
              return;
  
          Object[] pdus = (Object[]) extras.get("pdus");
          for (int i = 0; i &lt; pdus.length; i++) {
              SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
              String sender = SMessage.getOriginatingAddress();
              String body = SMessage.getMessageBody().toString();
  
              message = mySQLiteAdapter.createMessage(sender, body);
  
              // A custom Intent that will used as another Broadcast
              Intent in = new Intent("SmsMessage.intent.MAIN").putExtra(
                      "get_msg", sender + ":" + body);
  
              // To display a Toast whenever there is an SMS.
              Toast.makeText(context, body, Toast.LENGTH_LONG).show();
  
              Uri personUri = Uri.withAppendedPath( ContactsContract.PhoneLookup.CONTENT_FILTER_URI, SMessage.getOriginatingAddress());  
  
              Cursor cur = context.getContentResolver().query(personUri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null );  
  
              if( cur.moveToFirst() ) {  
                           int nameIndex = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);  
  
                           String PersonName = cur.getString(nameIndex); 
  
                           Toast.makeText(context, PersonName, Toast.LENGTH_LONG).show();
              }
              cur.close();
  
              // You can place your check conditions here(on the SMS or the
              // sender)
              // and then send another broadcast
              context.sendBroadcast(in);
  
              // This is used to abort the broadcast and can be used to silently
              // process incoming message and prevent it from further being
              // broadcasted. Avoid this, as this is not the way to program an
              // app.
              this.abortBroadcast();
          }
      }
  }
  </code></pre>
  
  <p>This is the code that I have added that cause my app to crash:</p>
  
  <pre><code>Uri personUri = Uri.withAppendedPath( ContactsContract.PhoneLookup.CONTENT_FILTER_URI, SMessage.getOriginatingAddress());  
  
  Cursor cur = context.getContentResolver().query(personUri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null );  
  
  if( cur.moveToFirst() ) {  
               int nameIndex = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);  
  
               String PersonName = cur.getString(nameIndex); 
  
               Toast.makeText(context, PersonName, Toast.LENGTH_LONG).show();
  }
  cur.close();
  </code></pre>
  
  <p>I just modified the code from the answer on this <a href="https://stackoverflow.com/questions/4359564/how-to-get-the-contacts-display-name-in-sms-uri">link</a>.</p>
  
  <p>Now the app crashes when receiving an SMS.</p>