-1

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

Kick Buttowski
  • 6,631
  • 13
  • 35
  • 57

3 Answers3

2

Change

if (names[j].compareTo(names[j - 1]) > 0) { 

to

if (names[j] != null && names[j].compareTo(names[j - 1]) > 0) {

And the annoying null pointer exception will go away.

If you ever get over your 500 String array obsession I suggest you try TreeSet since it will do all the sorting work for you.

Cheat Sheet

public static void main(String[] args)
{
    Set<String> alphabetical = new TreeSet<String>();
    alphabetical.add("A");
    alphabetical.add("Z");
    alphabetical.add("M");

    System.out.println(alphabetical);
}

outputs: [A, M, Z]

candied_orange
  • 6,382
  • 2
  • 25
  • 61
0

Your names array has 500 elements, most of which are null. That's why you are getting NullPointerException, when you call names[j].compareTo() for a null reference.

You should only attempt to sort as many names as you get as input.

Instead of

int namesLength = names.length;

Try

int namesLength = count;

Since count holds the number of inputs you actually have.

BTW, your sort() method has other problems :

  1. The loops should go from 0 to namesLength - 1, so the condition should be j < namesLength
  2. names [j-1] would give you ArrayIndexOutOfBoundsException when j==0
Eran
  • 374,785
  • 51
  • 663
  • 734
0

You have the array in size 500 and number your names are 6.

when you assign six names to first six indexes of the array, the rest indexes are still having null value. Therefor, comparing with the null value will throw NullPointerException.

why?

Because Objects in Java initialized by null value when they are defined for the first time.

Suggestion :

Try to use ArrayList which shrinks and expands by itself

Kick Buttowski
  • 6,631
  • 13
  • 35
  • 57