I am attempting to sort a list of names in alphabetical order and I keep getting the error Exception in thread "main" java.lang.NullPointerException and I don't know why.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class alphabeticalOrder {
static String names[];
static int count = 0;
static String sorting;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String[] names = new String[500];
File namesFile = new File("names.txt");
Scanner inputFile = new Scanner(namesFile);
while (inputFile.hasNextLine()) {
String line = inputFile.nextLine();
String[] namesDetails = line.split(" ");
names[count] = namesDetails[0];
count++;
}
sort();
System.out.println(Arrays.toString(names));
}
public static void sort() {
int namesLength = names.length;
for (int i = 0; i < namesLength - 1; i++) {
for (int j = 0; j < namesLength - 1; j++) {
if (names[j].compareTo(names[j - 1]) > 0) {
sorting = names[j - 1];
names[j - 1] = names[j];
names[j] = sorting;
}
}
}
}
}
Customers txt has these names
Smith, Alexandra
Downes, Trish
Akbal, Maria
and the array must equal 500