1

This program reads the lines of an input file and stores them into the array words. Then each element in words[] is put into a character array and sorted alphabetically. Each sorted character array is assigned to a string and those strings populate another array sortedWords. I need to sort the elements in the sortedWords array. I get a NullPointerException when I use Arrays.sort(sortedWords).

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter a file name: ");
    System.out.flush();

    String filename = scanner.nextLine();
    File file = new File(filename);

    String[] words = new String[10];
    String[] sortedWords = new String[10];

    try {   
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);

        String line = br.readLine();
        int i = 0;

        while(line != null) {
            words[i] = line.toString();    // assigns lines into the array
            line = br.readLine();    // this will eventually set line to null, terminating the loop

            String signature = words[i];                        
            char[] characters = signature.toCharArray();        
            Arrays.sort(characters);
            String sorted = new String(characters);               

            sortedWords[i] = sorted;    // assigns each signature into the array
            sortedWords[i] = sortedWords[i].replaceAll("[^a-zA-Z]", "").toLowerCase();    // removes non-alphabetic chars and makes lowercase
            Arrays.sort(sortedWords); 

            System.out.println(words[i] + " " + sortedWords[i]);
            i++;
        }     
    } 

    catch(IOException e) {
        System.out.println(e);
    }
}
2
  • Hopefully your file only has 10 lines. Commented Sep 24, 2013 at 17:33
  • @SotiriosDelimanolis yep Commented Sep 24, 2013 at 17:38

1 Answer 1

3

You should take the sort out of the while loop.

int i = 0;
while(line != null) {
   ...
   i++;
}
Arrays.sort(sortedWords, 0, i); 

The problem is that you call sort before you finished populating the sortedWords array. The array therefore still contains null strings and these cause the NullPointerException.

Sign up to request clarification or add additional context in comments.

6 Comments

Still gives me a NullPointerException
The input file will also need to have exactly 10 lines for this to work. If there are more than 10 the array index will be out of bounds and if there are less than 10, the same null pointer exception will occur
Your file probably doesn't have 10 lines. Consider using List<String> and Collections.sort
@G_A How can I account for more/less than 10 lines while still using an array? I understand an ArrayList or Collection is a better data structure but I'm limited to Arrays (homework).
@dk1 Updated answer. You can give Arrays.sort a start and end index.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.