I have an array with two strings. I want to sort it by Alphabetical order. So this is the following code.
ArrayList<String> a = new ArrayList<> ();
j = (String) b.get ("user");
a.add (j);
chatroomname.setText (j);
a.add (Fusername);
Log.i ("Size Of A ", String.valueOf (a.size ()));
Collections.sort (a, new Comparator<String> () {
@Override
public int compare(String o1, String o2) {
return o1.compareToIgnoreCase (o2);
}
});
I also logged the size of array on my console. And it's showing 2. Which is correct. So I don't understand why I'm getting this nullpointerexception.
Here is my error log:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.buckydroid.anonchat/com.buckydroid.anonchat.ChatRoom}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.compareToIgnoreCase(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.compareToIgnoreCase(java.lang.String)' on a null object reference
at com.buckydroid.anonchat.ChatRoom$5.compare(ChatRoom.java:244)
at com.buckydroid.anonchat.ChatRoom$5.compare(ChatRoom.java:242)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:351)
at java.util.TimSort.sort(TimSort.java:216)
at java.util.Arrays.sort(Arrays.java:1523)
at java.util.Collections.sort(Collections.java:238)
at com.buckydroid.anonchat.ChatRoom.onCreate(ChatRoom.java:241)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Thanx!!!