-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,709
  • 13
  • 37
  • 58

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
  • 7,036
  • 2
  • 28
  • 62
  • I tried both ways and am still having the error – programmingHelp Oct 26 '14 at 08:16
  • On exactly what line? – candied_orange Oct 26 '14 at 08:17
  • if (names[j] != null && names[j].compareTo(names[j - 1]) > 0) { this and the sort part in the main method where i have the method sort() – programmingHelp Oct 26 '14 at 08:20
  • If you haven't simply forgotten to compile like this guy: http://stackoverflow.com/questions/1816776/java-short-circuit-evaluation then do a System.out.println(names[j] + "<- names[j]")//TODO remove. Put it just before the if. – candied_orange Oct 26 '14 at 08:25
  • I am doing that and I am still getting the error and I have no idea why because it looks right but just won't compile – programmingHelp Oct 26 '14 at 08:33
  • Does it display the names[j] + "<- names[j]" line once or 500x500 times? -- Wait a minute, won't compile? Null pointer exception is a run time error not a compiler error. What are you really seeing? – candied_orange Oct 26 '14 at 08:41
  • Exception in thread "main" java.lang.NullPointerException at final_practice.alphabeticalOrder.sort(alphabeticalOrder.java:46) at final_practice.alphabeticalOrder.main(alphabeticalOrder.java:35) this is what i am seeing – programmingHelp Oct 26 '14 at 08:43
  • That's no compiler error. Did you add the <- names[j] line? Did you recompile successfully after adding it? When you rerun the code did it display? What did it display? – candied_orange Oct 26 '14 at 08:46
  • it says the exact same thing . This is what you ment rite public static void sort() { int namesLength = count; for (int i = 0; i < namesLength; i++) { for (int j = 0; j < namesLength; j++) { System.out.println(names[j] + "<- names[j]"); if(names[j] != null && names[j].compareTo(names[j - 1]) > 0) { sorting = names[j - 1]; names[j - 1] = names[j]; names[j] = sorting; } } } } – programmingHelp Oct 26 '14 at 08:48
  • Then you didn't add the line, didn't add it before the error, or you didn't recompile. And yes that's what I meant. Try adding a System.out.println("Hello World"); at the very start of your code. If it works move it down until it doesn't. If it doesn't work you're not compiling like you think you are. – candied_orange Oct 26 '14 at 08:49
  • it works everywhere but inside the if statement – programmingHelp Oct 26 '14 at 08:55
  • Well if Hello World works outside the if then System.out.println(names[j] + "<- names[j]"); will work outside the if. Run it just before the if and tell me what you see. Scroll up to find its output if you have to. – candied_orange Oct 26 '14 at 08:59
  • nothing comes up just the null pointer error – programmingHelp Oct 26 '14 at 09:17
  • It should be displaying `null <- names[j]` at least. If you're telling me switching from Hello World to the debugging code magically makes it stop displaying then you've got me at a loss because I've just tested that code on my end at it works fine. – candied_orange Oct 26 '14 at 09:20
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
  • 387,369
  • 54
  • 702
  • 768
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,709
  • 13
  • 37
  • 58