NullPointerException when running on the phone (but not on emulator)
<p>I want to display a list of the contact names from the SMS inbox and outbox (like in the native messaging app). I have come up with the following code:</p>

<pre><code>public void onCreate(Bundle icicle) {

super.onCreate(icicle);

Uri messagesUri = Uri.parse("content://sms/");
Cursor cursor = getContentResolver().query(messagesUri,new String[] { "_id", "thread_id", "address", "person", "date", "body", "type" }, null, null, null);
startManagingCursor(cursor);
String[] columns = new String[] { "address", "person", "date", "body", "type" };
String sms = "";
if (cursor.getCount() &gt; 0) {
   while (cursor.moveToNext()){
       String address = cursor.getString(cursor.getColumnIndex(columns[0]));
       sms += address + " ";
       String contact=address;
       Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));  
       Cursor cs= getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
       startManagingCursor(cs);
       if(cs.getCount()&gt;0)
       {
        cs.moveToFirst();
        contact=cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
       } 
       listItems.add(contact);
   }
}
adapter=new ArrayAdapter&lt;String&gt;(this,
    android.R.layout.simple_list_item_1,
    listItems);
setListAdapter(adapter);
}
</code></pre>

<p>This works when I run the application in an emulator, but when I try to run it on a phone I get a NullPointerException. If I double-click on the error message in the LogCat the following line gets highlighted:</p>

<pre><code>if(cs.getCount()&gt;0)
</code></pre>

<p>What is the problem here?</p>
