Hi,

In my application, I am creating a string array in a method. Each time I call that method this array will get created and also a null pointer exception is showing when I use the Arrays.sort() method.

My method is like this...

int count = getTotalXMLCount();
 envArray = new String[count];

 /* code for adding the elements into the array from a file */

 Arrays.sort(envArray, String.CASE_INSENSITIVE_ORDER);
 for (int i = 0; i < envArray.length; i++) 
         variableCombo.addItem(envArray[i]);

First calling it works perfectly. But the second calling, it shows the exception

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at java.lang.String$CaseInsensitiveComparator.compare(Unknown Source)
	at java.lang.String$CaseInsensitiveComparator.compare(Unknown Source)
	at java.util.Arrays.mergeSort(Unknown Source)

In my conclusion it is due the line:

envArray = new String[count];

How can I delete this array during the second time. Can anybody help me in this issue pls...? Its urgent....

THANKS in Advance....

Hi, this is happening because no values have been assigned to the array, and hence it is throwing NullPointerException. Assign individual values to all the elements in the array and try again

Hi, this is happening because no values have been assigned to the array, and hence it is throwing NullPointerException. Assign individual values to all the elements in the array and try again

Ya, Thank you... I found the mistake...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.