0

I'm learning about arrays in java and have put together a program that loads a text file into an array of strings. The user is then prompted to search for a string.

The program compiles just fine, but when I run it, I get the following error:

Exception in thread "main" java.lang.NullPointerException
    at WordsArray.main(WordsArray.java:60)

Line 60 is as follows:

if (words[index].compareTo(words[minIndex]) > 0)

I wonder if it has something to do with creating instances of my words array? I'm not sure where to take that idea (I thought the given instance would be created within the iteration), so any suggestions are welcome!

Here is my code:

import java.util.*;
import java.io.*;

public class WordsArray
{
   public static void main(String[] args) throws IOException
   {
      final int NUM_WORDS = 100;                // Number of words read from file
      String[] words = new String[NUM_WORDS];   // Array of words
      int index = 0;                            // Control the loop

      // Open the file.
      File file = new File("words.txt");
      Scanner inputFile = new Scanner(file);

      // Read the file contents to an array
      while (inputFile.hasNext() && index < words.length)
      {
         words[index] = inputFile.nextLine();
         index++;
      }

      // Close the file.
      inputFile.close();

      // Ask the user to search for a word
      Scanner keyboard = new Scanner(System.in);   // Create Scanner object
      System.out.println("Enter the word to search: ");
      String value;
      value = keyboard.nextLine(); 

      // Selection sort
      int startScan = 0;
      int minIndex;
      String firstValue;

      for (startScan = 0; startScan < (words.length - 1); startScan++)
      {
         minIndex = startScan;
         firstValue = words[startScan];
         for (index = startScan + 1; index < words.length; index++)
         {
            if (words[index].compareTo(words[minIndex]) > 0)
            {
               //firstValue = words[index];
               minIndex = index;
            }
         }
         words[startScan] = words[minIndex];
         words[minIndex] = firstValue;
      }

      // Binary Search
      int first;        // First array element
      int last;         // Last array element
      int middle;       // Middle point of search
      int position;     // Position of search value
      boolean found;    // Flag

      // Set initial values
      first = 0;
      last = words.length -1;
      position = -1;
      found = false;

      // Search for the value
      while (!found && first <= last)
      {
         // Calculate midpoint
         middle = (first + last) / 2;

         if (words[middle].equals(value)) // if value is in middle
         {
            found = true;
            position = middle;
         }
         else if (words[middle].compareTo(value) > 0)  // if value is in lower half
         {
            last = middle - 1;
         }
         else                             // if value is in upper half
         {
            first = middle + 1;
         }
      }

      // Print index of search value, or -1 if not found
      if (found = true)
      {
         System.out.println(value + " was found at " + position);
      }
   }
}

1 Answers1

1

The condition:

while (inputFile.hasNext() && index < words.length)

will stop if inputFile doesn't have "next", but later in the for-loop, you don't take into account that the input might have finished before the array got filled up:

for (startScan = 0; startScan < (words.length - 1); startScan++)
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129