Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6779697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:25:00+00:00 2026-05-26T16:25:00+00:00

I am trying to have name of contacts in one array and their types

  • 0

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 line where I am getting null pointer exception.please help..thanks in advance.

package application.test;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;  
import android.provider.ContactsContract.Contacts.Data;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

public final class TestActivity extends Activity {
String[] name;
String[] phoneType;
ListView lv;
ListViewAdapter lva;


    public static final String TAG = "ContactManager";
@Override
public void onCreate(Bundle savedInstanceState)
{
    Log.v(TAG, "Activity State: onCreate()");
    super.onCreate(savedInstanceState);
    LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);        
    LinearLayout mainLayout=new LinearLayout(this);
    mainLayout.setOrientation(LinearLayout.VERTICAL);               
    LayoutInflater layoutInflater = getLayoutInflater();        
    mainLayout.addView(layoutInflater.inflate(R.layout.main,null));
    mainLayout.addView(layoutInflater.inflate(R.layout.extra,null));

    this.addContentView(mainLayout, params);

      lv = (ListView)findViewById(android.R.id.list);
     lva = new ListViewAdapter(this,name,phoneType); 
    lv.setAdapter(lva);
    testGetContacts();
}


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);

          while (cur.moveToNext()) {
             int  i=1;
              String id = cur.getString(indexID);    
 //HERE LIES NULL POINTER EXCEPTION   name[i] = cur.getString(indexName);  
 //HERE TOO              phoneType[i] =  cur.getString(indexPhoneType);

             i++;


              System.out.println(id + "\n");
              System.out.println(name + "\n");
              System.out.println(phoneType + "\n");


          }


        } catch (SQLException sqle) {
           //handling exception       
        } finally { 
         if (!cur.isClosed()) {
             cur.close();
         }     
     }

        }

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:25:01+00:00Added an answer on May 26, 2026 at 4:25 pm

    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++;
    }    
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get this query to work but I am stumbed
I'm trying to have Screenreaders (I'm testing with NVDA) read out the AutomationProperties.Name and
In Android, I'm trying trying to have the user enter a place they name,
I have a bunch of links with the class name row. What I'm trying
We have an url like http://site.s3.amazonaws.com/images/some image @name.jpg inside $string What I'm trying to
I have the following database structure. ID | Name | Marks I am trying
I have two tables as shown here . I am trying to fetch name
I have the following code: Tag.find_all_by_company_id(4).each.collect{|tag| tag.name }.join(,) (Essentially I'm trying to build a
I'm trying to get familiar with Collections. I have a String which is my
I'm trying to send an email merging one document (.docx) with a contacts database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.