Results 1 to 6 of 6
- 03-27-2011, 04:41 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
using selection sort on 2 string arrays
here is my code, for a selection sort on 2 string arrays returning nothing. i have 2 string arrays loaded from a file now i need to sort them. but everytime i run this i get a null pointer exception
public static void selectionSort(String[] array1, String[] array2)
{
int startScan, index, minIndex;
String minValue1, minValue2;
for (startScan = 0; startScan < (array1.length-1); startScan++)
{
minIndex = startScan;
minValue1 = array1[startScan];
minValue2 = array2[startScan];
for(index = startScan + 1; index < array1.length; index++)
{
if (array1[index].compareTo(minValue1) < 0)
{
minValue1 = array1[index];
minValue2 = array2[index];
minIndex = index;
}
}
array1[minIndex] = array1[startScan];
array2[minIndex] = array2[startScan];
array1[startScan] = minValue1;
array2[startScan] = minValue2;
}
}
can anyone please help me
here is my code to call from a file and load the array then attempt to sort and print out the result of the sort
public CommissionReport() throws IOException
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the file name: ");
String inputtedFileName = keyboard.nextLine();
File fileName = new File(inputtedFileName);
if(fileName.exists())
{
Scanner inputFile = new Scanner(fileName);
while(inputFile.hasNext())
{
for (int index = 0; index < ARRAY_SIZE; index++)
{
salesId[index] = inputFile.nextLine();
salesName[index] = inputFile.nextLine();
inputFile.close();
System.out.println(salesId[index]);
System.out.println(salesName[index]);
}
MyArrays.selectionSort(salesId, salesName);
}
}
else
{
System.out.println("File does not exist");
}
- 03-27-2011, 05:00 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
everytime i run this i get a null pointer exception
A couple of things: first, when you post code, use the code tags. You put [code] at the start of your code and [/code] at the end. That way the indentation is preserved and the code remains readable.
Secondly if you get a runtime exception like a NullPointerException it is a good idea to copy and post the entire error message. It will refer to a line number in your code and you should say which line that is.
- 03-27-2011, 05:12 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
oh sorry new to this
Java Code:public static void selectionSort(String[] array1, String[] array2) { int startScan, index, minIndex; String minValue1, minValue2; for (startScan = 0; startScan < (array1.length-1); startScan++) { minIndex = startScan; minValue1 = array1[startScan]; minValue2 = array2[startScan]; for(index = startScan + 1; index < array1.length; index++) { [B][U]if (array1[index].compareTo(minValue1) < 0)[/U][/B] { minValue1 = array1[index]; minValue2 = array2[index]; minIndex = index; } } array1[minIndex] = array1[startScan]; array2[minIndex] = array2[startScan]; array1[startScan] = minValue1; array2[startScan] = minValue2; } }
Java Code:public CommissionReport() throws IOException { Scanner keyboard = new Scanner(System.in); System.out.print("Enter the file name: "); String inputtedFileName = keyboard.nextLine(); File fileName = new File(inputtedFileName); if(fileName.exists()) { Scanner inputFile = new Scanner(fileName); while(inputFile.hasNext()) { for (int index = 0; index < ARRAY_SIZE; index++) { salesId[index] = inputFile.nextLine(); salesName[index] = inputFile.nextLine(); inputFile.close(); System.out.println(salesId[index]); System.out.println(salesName[index]); } MyArrays.selectionSort(salesId, salesName); } } else { System.out.println("File does not exist"); }
java.lang.nullpointerexception : null
the highlighted code is
if (array1[index].compareTo(minValue1) < 0)
- 03-27-2011, 05:35 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
It looks like array1[index] is null. You can verify this by using System.out.println().
Java Code:public static void selectionSort(String[] array1, String[] array2) { int startScan, index, minIndex; String minValue1, minValue2; for (startScan = 0; startScan < (array1.length-1); startScan++) { minIndex = startScan; minValue1 = array1[startScan]; minValue2 = array2[startScan]; for(index = startScan + 1; index < array1.length; index++) { [color=blue]if(array1[index] == null) { System.out.println("array1 is null at index=" + index); }[/color] if (array1[index].compareTo(minValue1) < 0) { minValue1 = array1[index]; minValue2 = array2[index]; minIndex = index; } } array1[minIndex] = array1[startScan]; array2[minIndex] = array2[startScan]; array1[startScan] = minValue1; array2[startScan] = minValue2; } }
Once you have verified that this is the source of the NullPointerException you have to go back to whereever array1 got its values and see why there is a null at that point. (You should also verify that the value of index is reasonable: ie that you really expect the array to have a nonnull value at that point.)
- 03-27-2011, 05:42 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
You populate the salesId array with code like this:
Java Code:while(inputFile.hasNext()) { for (int index = 0; index < ARRAY_SIZE; index++) { salesId[index] = inputFile.nextLine();
Can you see that the array may very well end up with nulls in it? If not, print it.
Java Code:if(fileName.exists()) { // etc } else { System.out.println("File does not exist"); } [color=blue]System.out.println("The salesId array:"); for(int i = 0; i < salesId.length;i++) { System.out.print(salesId[i] + " "); } System.out.println();[/color] // etc
- 03-27-2011, 05:59 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Similar Threads
-
Selection Sort. please help!
By cassato in forum New To JavaReplies: 4Last Post: 03-14-2011, 11:26 PM -
Is this a Selection Sort?
By Metastar in forum New To JavaReplies: 2Last Post: 10-22-2010, 06:00 AM -
selection sort
By mayhewj7 in forum New To JavaReplies: 1Last Post: 04-29-2009, 01:40 AM -
How to change Bubble+Selection+Insertion Sorts to Sort String Values?
By VinceGuad in forum New To JavaReplies: 3Last Post: 01-26-2009, 01:20 AM -
Selection sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 08:41 PM
Bookmarks