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
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 sa=new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,s);
lv.setAdapter(sa);
}
else
{
Toast.makeText(getApplicationContext(),"Nothing Found",Toast.LENGTH_SHORT).show();
}