字符串数组中的java.lang.NullPointerException?

时间:2015-03-10 01:44:54

标签: java arrays nullpointerexception

我正在学习java中的数组,并将一个将文本文件加载到字符串数组中的程序放在一起。然后提示用户搜索字符串。

程序编译得很好,但是当我运行它时,我收到以下错误:

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

第60行如下:

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

我想知道它是否与创建我的单词数组的实例有关?我不确定在哪里采取这个想法(我认为给定的实例将在迭代中创建),所以欢迎任何建议!

这是我的代码:

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 个答案:

答案 0 :(得分:1)

条件:

while (inputFile.hasNext() && index < words.length)
如果inputFile没有“next”,

将停止,但是在for循环中稍后,你没有考虑输入可能在数组填满之前完成:

for (startScan = 0; startScan < (words.length - 1); startScan++)