I am trying to have name of contacts in one array and their types in another array,but can\'t get through with null pointer exception.here is my code.I have pointed out the
Try this and tell me i it helped:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView)findViewById(R.id.listview);
testGetContacts();
ArrayAdapter<String> sa=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,s);
lv.setAdapter(sa);
}//method
private void testGetContacts() {
ContentResolver cr = getContentResolver();
String[] projection = new String[] { Data._ID,
ContactsContract.Contacts.DISPLAY_NAME, Phone.TYPE};
Cursor cur = cr.query(ContactsContract.Data.CONTENT_URI,
projection, null, null, null);
if (cur != null && cur.moveToFirst()) {
try {
int indexID = cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
int indexName = cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
int indexPhoneType = cur.getColumnIndexOrThrow(Phone.TYPE);
name=new String[cur.getCount()];
phoneType=new String[cur.getCount()];
s=new String[cur.getCount()];
int i=1;
while (cur.moveToNext()) {
String id = cur.getString(indexID);
name[i] = cur.getString(indexName);
phoneType[i] = cur.getString(indexPhoneType);
String temp="id="+id+"-name="+name[i]+"-phoneType="+phoneType[i];
s[i-1]=temp;
i++;
}//while
ArrayAdapter<String> sa=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,s);
lv.setAdapter(sa);
}catch(Exception e){
e.printStackTrace();
}//catch
}//if
}//method
Initialize String[]
name before using it.
You can do that like:
name=new String[cur.getCount()];
String s="";
while (cur.moveToNext()) {
int i=1;
String id = cur.getString(indexID);
name[i] = cur.getString(indexName);
phoneType[i] = cur.getString(indexPhoneType);
//System.out.println(id + "\n");
//System.out.println(name + "\n");
//System.out.println(phoneType + "\n");
s=s+"id="+id+" name="+name[i]+" phoneType="+phoneType[i]+"\n";
i++;
}
Toast.makeText(getApplicationContext(),i+" - "+s).show();
Edit :
Create an xml file in layout folder.
main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listview"
android:cacheColorHint="#0000"
/>
</LinearLayout>
now in your TestActivity.class:
public final class TestActivity extends Activity {
String[] name;
String[] phoneType;
ListView lv;
String s[];
public static final String TAG = "ContactManager";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.id.main);
testGetContacts();
lv = (ListView)findViewById(R.id.listview);
ArrayAdapter<String> sa=new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,s);
lv.setAdapter(sa);
}
private void testGetContacts() {
ContentResolver cr = getContentResolver();
String[] projection = new String[] { Data._ID,
ContactsContract.Contacts.DISPLAY_NAME, Phone.TYPE};
Cursor cur = cr.query(ContactsContract.Data.CONTENT_URI,
projection, null, null, null);
if (cur != null && cur.moveToFirst()) {
try {
int indexID = cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
int indexName = cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
int indexPhoneType = cur.getColumnIndexOrThrow(Phone.TYPE);
name=new String[cur.getCount()];
s=new String[cur.getCount()];
while (cur.moveToNext()) {
int i=1;
String id = cur.getString(indexID);
name[i-1] = cur.getString(indexName);
phoneType[i-1] = cur.getString(indexPhoneType);
String temp="id="+id+"-name="+name[i-1]+"-phoneType="+phoneType[i-1];
s[i-1]=temp;
i++;
}
}
You're not initializing your String[] name, so when you try to access it, you get a null pointer exception. I would suggest using more meaningful variable names. 'name' is very ambiguous.
Your name and phone arrays have not been initialized.
String[] name = new String[EXPECTED_SIZE];
String[] phoneType = new String[EXPECTED_SIZE];
Any proper IDE should tell you that before you try to run it. Are you using Eclipse or IntelliJ? You should!
You are getting null pointer because you are accessing wrong index.
try this:
s=new String[cur.getCount()];
int i=1;
while (cur.moveToNext()) {
String id = cur.getString(indexID);
name[i-1] = cur.getString(indexName);
phoneType[i-1] = cur.getString(indexPhoneType);
String temp="id="+id+"-name="+name[i-1]+"-phoneType="+phoneType[i-1];
s[i-1]=temp;
i++;
}
and also lv=(ListView)findViewById(R.id.listview);
instead of lv=(ListView)findViewById(android.R.id.list);
You should add a condition here:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testGetContacts();
if(s.length()>0)
{
lv = (ListView)findViewById(R.id.listview);
ArrayAdapter<String> sa=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,s);
lv.setAdapter(sa);
}
else
{
Toast.makeText(getApplicationContext(),"Nothing Found",Toast.LENGTH_SHORT).show();
}