Click here to Skip to main content
12,504,695 members (64,090 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Mobile Android
Hi,

I want to get the mobile contacts and view in the listView. But i am facing error.
It will be shown below,
01-07 12:43:49.381: E/AndroidRuntime(863): FATAL EXCEPTION: main
01-07 12:43:49.381: E/AndroidRuntime(863): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rathnasoft.sample/com.rathnasoft.sample.ContactsPhone}: java.lang.NullPointerException
01-07 12:43:49.381: E/AndroidRuntime(863): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at android.os.Looper.loop(Looper.java:123)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at android.app.ActivityThread.main(ActivityThread.java:4627)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at java.lang.reflect.Method.invokeNative(Native Method)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at java.lang.reflect.Method.invoke(Method.java:521)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at dalvik.system.NativeStart.main(Native Method)
01-07 12:43:49.381: E/AndroidRuntime(863): Caused by: java.lang.NullPointerException
01-07 12:43:49.381: E/AndroidRuntime(863): 	at com.rathnasoft.sample.ContactsPhone.displayContacts(ContactsPhone.java:67)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at com.rathnasoft.sample.ContactsPhone.onCreate(ContactsPhone.java:22)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 12:43:49.381: E/AndroidRuntime(863): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-07 12:43:49.381: E/AndroidRuntime(863): 	... 11 more
And the coding part is:
public class ContactsPhone extends Activity {
	private ListView lvContact;
 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contacts);
		lvContact = (ListView) findViewById(R.id.listView1);
		ArrayList<string> list = displayContacts();
		ArrayAdapter<string> adapter = new ArrayAdapter<string>(getBaseContext(), android.R.layout.simple_list_item_1,list);
		lvContact.setAdapter(adapter);
		
 
	}
 
	private ArrayList<string> displayContacts() {
 
		ContentResolver cr = getContentResolver();
		Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
				null, null, null);
		
		ArrayList<string> contact = null;
		String phoneNo = null;
		if (cur.getCount() > 0) {
			while (cur.moveToNext()) {
				String id = cur.getString(cur
						.getColumnIndex(ContactsContract.Contacts._ID));
				String name = cur
						.getString(cur
								.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
 
				if (Integer
						.parseInt(cur.getString(cur
								.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
					Cursor pCur = cr.query(
							ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
							null,
							ContactsContract.CommonDataKinds.Phone.CONTACT_ID
									+ " = ?", new String[] { id }, null);
					while (pCur.moveToNext()) {
						phoneNo = pCur
								.getString(pCur
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
 
						System.out.println("name" + name + "ph no" + phoneNo);
						Toast.makeText(this,
								"Name: " + name + ", Phone No: " + phoneNo,
								Toast.LENGTH_SHORT).show();
					}
					
 
					pCur.close();
				}
				contact.add(name + "\n" + phoneNo);
			}
		}
		return contact;
 
	}
}</string>
Posted 6-Jan-13 20:16pm
Updated 7-Jan-13 5:19am
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Well you have a null pointer exception so your problem could be

ArrayList<string> contact = null;

try setting it to

ArrayList<string> contact =  new ArrayList<string>();

see if that helps

/Darren
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160919.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100