搜索文本文件时抛出`NullPointerException`

时间:2015-03-29 02:05:46

标签: java java.util.scanner

首先,我在下面的**中输入了一行NullPointerException。 其次,我的程序输出错误(我不知何故让它工作,但后来又回到了错误状态)。它必须是逻辑错误。我有一个11行的文件directory.txt,每个行都有一个名字。当我运行我的程序试图找到某个名称时,它只找到第一行的第一个名字和其他一切,它找不到。如何修复这2个错误?

我有2节课。这是第一类目录:

import java.util.*;
import java.io.*;
public class Directory {
   //public static void main(String[] args) {
   final int maxDirectorySize = 1024;
   String directory[] = new String[maxDirectorySize];
   int directorySize = 0;
   File directoryFile = null;
   Scanner directoryDataIn = null;

   public Directory(String directoryFileName) {
      directoryFile = new File(directoryFileName);
      try {
         directoryDataIn = new Scanner(directoryFile);
      }
      catch (FileNotFoundException e) {
         System.out.println("File is not found, exiting!" + directoryFileName);
         System.exit(0);
      }
      while (directoryDataIn.hasNext()) {
         directory[directorySize++] = directoryDataIn.nextLine();
      }
   }
   public boolean inDirectory(String name) {
      boolean inDir = true;
      for (int i = 0; i < directory.length; i++) {
         **if (directory[i].equalsIgnoreCase(name))** 
            inDir = true;
         else 
            inDir = false;
      }
      return inDir;
   }
   public boolean add(String name) {
      if (directory.length == 1024)
         return false;
      for (int i = 0; i < directory.length; i++) {
         if (directory[i].equalsIgnoreCase(name))
            return false;
         else
            directory[directorySize++] = name;
            return true;
      }
      return false;
   }          

   public boolean delete(String name) {
      for (int i = 0; i < directory.length; i++) {
         if (directory[i].equalsIgnoreCase(name)) {
            directory[i] = null;
            return true;
         }   
         else
            return false;
      }
      return false;
   }

   public void closeDirectory() {
      directoryDataIn.close();
      PrintStream directoryDataOut = null;
      try {
          directoryDataOut = new PrintStream(directoryFile);
      }
      catch (FileNotFoundException e) {
         System.out.printf("File %s not found, exiting!", directoryFile);
         System.exit(0);
      }
      String originalDirectory[] = {"Mike","Jim","Barry","Cristian","Vincent","Chengjun","susan","ng","serena"};
      if (originalDirectory == directory)
         System.exit(0);
      else
         for (int i = 0; i < directorySize; i++)
            directoryDataOut.println(directory[i]);
         directoryDataOut.close();
   }
}

这是我正在尝试运行的第二个类但是我一直得到异常主线程NullPointerException。

import java.io.*;
import java.util.*;
public class DirectoryWithObjectDesign {
   public static void main(String[] args) throws IOException {
   String directoryDataFile  = "Directory.txt";
   Directory d = new Directory(directoryDataFile);
   Scanner stdin = new Scanner(System.in);
   System.out.println("Directory Server is Ready!");
   System.out.println("Format: command name");
   System.out.println("Enter ^Z to end");
   while (stdin.hasNext()) {
      String command = stdin.next();
      String name = stdin.next();
      if (command.equalsIgnoreCase("find")) {
         if (d.inDirectory(name))
            System.out.println(name + " is in the directory");
         else 
            System.out.println(name + " is NOT in the directory");
      }
      else if (command.equalsIgnoreCase("add")) {
         if (d.add(name))
            System.out.println(name + " added");
         else 
            System.out.println(name + " cannot add! " + "no more space or already in directory");
      }
      else if (command.equalsIgnoreCase("delete")) {
         if (d.delete(name))
            System.out.println(name + " deleted");
         else
            System.out.println(name + " NOT in directory");
      }
      else {
         System.out.println("bad command, try again");
      }
   }
   }
}   

2 个答案:

答案 0 :(得分:3)

此代码:

  while (directoryDataIn.hasNext()) {
     directory[directorySize++] = directoryDataIn.nextLine();
  }

只会填充directory与输入文件中的行数一样多(根据您的问题,为11行)。

此代码:

  for (int i = 0; i < directory.length; i++) {
     **if (directory[i].equalsIgnoreCase(name))** 

将遍历directory中的每个条目,直到其长度(1024)。

由于这些条目中的1013是null,因此尝试在其上运行equalsIgnoreCase()会产生NPE。

修改

您可以通过以下几种方式解决此问题。例如,你可以

  • 跟踪您阅读的行数,并且只读到那一点
  • 在评估之前检查每个条目以查看它是否为null
  • 使用动态大小的数据结构而不是数组,例如ArrayList
  • 检查已知值(例如if (name.equalsIgnoreCase(directory[i]))

答案 1 :(得分:0)

更改

for (int i = 0; i < directory.length; i++) {

for (int i = 0; i < directorySize; i++ ){

directorySize已经跟踪条目数,因此上面的任何数组条目都将为null。因此,尝试在它们上调用equalsIgnoreCase()将获得NPE。

实际上,这看起来像是ArrayList而不是数组的主要用途。列表将根据需要进行扩展,List.size()将为您提供正确的长度。