2

I,m trying to read all contacts from phone and try to sort the contact names, but calling this method Arrays.sort(mystring, String.CASE_INSENSITIVE_ORDER) causes fatal exception.

I don't know what the problem is..? Is there any other way to sort an String array in android(java)

public class ContactActivity extends Activity {

/**
 * @param args
 */
public static HashMap<String, String> myHash = new HashMap<String, String>();
public static String[] mystring = new String[10000];
protected static int i = 0;
protected Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contacts_screen);

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);

    //*************************************************
    getContactNumbers(this);
    Log.i("Aravinth", "...before sorting....");
    Arrays.sort(mystring, String.CASE_INSENSITIVE_ORDER);
    //*******************************************************

    final ListView contactLV = (ListView)findViewById(R.id.contactsListView);
    MyListAdapter listViewAdapter = new MyListAdapter(this,R.layout.list_items,myHash,mystring);
    contactLV.setAdapter(listViewAdapter);
    /*
    Arrays.sort(contactData);
    for(int i =0;i<contactData.length;i++)
    {
        Log.d("Aravinth",contactData[i]);
    }
    */
}

public static void getContactNumbers(Context context) {
    String contactNumber = null;
    String nameOfContact = null;
    Uri imageUri;
    int i = 0;

    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(BaseColumns._ID));
                nameOfContact = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", new String[] { id },null);

                    while (phones.moveToNext()) {
                        if(phones.getInt(phones.getColumnIndex(Phone.TYPE)) == Phone.TYPE_MOBILE)
                        {
                            contactNumber = phones.getString(phones.getColumnIndex(Phone.NUMBER));
                            mystring[i] = nameOfContact;
                            myHash.put(nameOfContact, nameOfContact);
                            Log.i("Aravinth", "...Contact Name ...." + mystring[i] + "...contact Number..." + contactNumber);
                        }
                    }
                    phones.close();
                }
                i++;
            }
        }// end of contact name cursor
        cur.close();
}

}

here is the log

08-03 00:45:25.247: I/Aravinth(676): ...before sorting....
08-03 00:45:25.267: D/AndroidRuntime(676): Shutting down VM
08-03 00:45:25.267: W/dalvikvm(676): threadid=1: thread exiting with uncaught exception (group=0x40a9b210)
08-03 00:45:25.287: E/AndroidRuntime(676): FATAL EXCEPTION: main
08-03 00:45:25.287: E/AndroidRuntime(676): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arsenic.test01/com.arsenic.test01.ContactActivity}: java.lang.NullPointerException
08-03 00:45:25.287: E/AndroidRuntime(676):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
08-03 00:45:25.287: E/AndroidRuntime(676):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
08-03 00:45:25.287: E/AndroidRuntime(676):  at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-03 00:45:25.287: E/AndroidRuntime(676):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
08-03 00:45:25.287: E/AndroidRuntime(676):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 00:45:25.287: E/AndroidRuntime(676):  at android.os.Looper.loop(Looper.java:137)
08-03 00:45:25.287: E/AndroidRuntime(676):  at android.app.ActivityThread.main(ActivityThread.java:4448)
08-03 00:45:25.287: E/AndroidRuntime(676):  at java.lang.reflect.Method.invokeNative(Native Method)
08-03 00:45:25.287: E/AndroidRuntime(676):  at java.lang.reflect.Method.invoke(Method.java:511)
08-03 00:45:25.287: E/AndroidRuntime(676):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
08-03 00:45:25.287: E/AndroidRuntime(676):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
08-03 00:45:25.287: E/AndroidRuntime(676):  at dalvik.system.NativeStart.main(Native Method)
08-03 00:45:25.287: E/AndroidRuntime(676): Caused by: java.lang.NullPointerException
08-03 00:45:25.287: E/AndroidRuntime(676):  at java.lang.String$CaseInsensitiveComparator.compare(String.java:89)
08-03 00:45:25.287: E/AndroidRuntime(676):  at java.lang.String$CaseInsensitiveComparator.compare(String.java:71)
08-03 00:45:25.287: E/AndroidRuntime(676):  at java.util.TimSort.binarySort(TimSort.java:261)
08-03 00:45:25.287: E/AndroidRuntime(676):  at java.util.TimSort.sort(TimSort.java:204)
08-03 00:45:25.287: E/AndroidRuntime(676):  at java.util.TimSort.sort(TimSort.java:169)
08-03 00:45:25.287: E/AndroidRuntime(676):  at java.util.Arrays.sort(Arrays.java:2038)
08-03 00:45:25.287: E/AndroidRuntime(676):  at com.arsenic.test01.ContactActivity.onCreate(ContactActivity.java:45)
08-03 00:45:25.287: E/AndroidRuntime(676):  at android.app.Activity.performCreate(Activity.java:4465)
08-03 00:45:25.287: E/AndroidRuntime(676):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-03 00:45:25.287: E/AndroidRuntime(676):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
08-03 00:45:25.287: E/AndroidRuntime(676):  ... 11 more
08-03 00:45:26.718: I/Process(676): Sending signal. PID: 676 SIG: 9
3
  • is there a stacktrace? Commented Aug 2, 2015 at 19:38
  • @SaschaKolberg log added. Commented Aug 2, 2015 at 19:44
  • instead of using such slow algorithm refer to this: stackoverflow.com/a/26820544/2252830 Commented Aug 2, 2015 at 20:07

3 Answers 3

1

08-03 00:45:25.287: E/AndroidRuntime(676): Caused by: java.lang.NullPointerException

Basically you are creating a big array of nulls and then trying to sort it -> NPE.

Correction Instead of an [] array, use a ArrayList which you can sort with Collections.sort

Sign up to request clarification or add additional context in comments.

Comments

1

You are calling getContactNumbers(this) on MainThread.

You have to use Asynctask for getting contacts and in its postExecute method you should call

Arrays.sort(mystring, String.CASE_INSENSITIVE_ORDER);
MyListAdapter listViewAdapter = new MyListAdapter(this,R.layout.list_items,myHash,mystring);
contactLV.setAdapter(listViewAdapter);

4 Comments

True but has nothing to do with the crash.
in mystring you have no data and you are performing sorting on that.
it works good but i need to pass context as the parameter which is not possible within AsyncTask<Void, Void, Void>() ..... how can do that..?
Instead of context you can use ContactActivity.this within the AsyncTask
0

More logs would be helpful, but here are two ideas that might help:

  1. I am not am 100% sure about this, but you might wanna check: You initialize your array with a size 10000 does that not mean that all entries are initialized as null? So if you do not have 10000 or more contacts, the array will have trailing null's, right?

  2. It may be that

    nameOfContact = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

    gives null for some contacts. At least the documentation does not explicitly exclude nullas return value.

    You could check for that

    if (nameOfContact == null) { nameOfContact = ""; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.