搜索文本文件时抛出`NullPointerException` [英] `NullPointerException` thrown while searching text file
问题描述
首先,我在下面的**行中不断获取NullPointerException. 其次,我的程序输出错误(我以某种方式使它工作,但随后又返回错误).它必须是逻辑错误.我有11行的文件directory.txt,每行上都有一个名称.当我运行程序尝试查找某个名称时,它只会在第一行中找到名字,其他都找不到.如何解决这两个错误?
First, I keep getting a NullPointerException on the line I put in ** below. Second, my program is giving the wrong output (I somehow got it to work but then it went back to error). It must be a logic error. I have a file directory.txt of 11 lines, each with a name on it. When I run my program to try to find a certain name, it only finds the first name on the first line and everything else, it can't find. How can I fix these 2 errors?
我有2节课.这是一流的目录:
I have 2 classes. This is the first class Directory:
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.
AND this is my second class which I'm trying to run but I keep getting exception main thread 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");
}
}
}
}
推荐答案
此代码:
while (directoryDataIn.hasNext()) {
directory[directorySize++] = directoryDataIn.nextLine();
}
只会填充输入文件中的行(根据您的问题,为11行)中的directory
条.
will only fill up as much of directory
as there are lines in the input file (11 according to your question).
此代码:
for (int i = 0; i < directory.length; i++) {
**if (directory[i].equalsIgnoreCase(name))**
将循环遍历directory
中的每个条目,直到其长度(1024).
will loop over every entry in directory
, up to its length (1024).
由于这些条目中的1013是null
,因此尝试在它们上运行equalsIgnoreCase()
会导致NPE.
Since 1013 of those entries are null
, trying to run equalsIgnoreCase()
on them will result in a NPE.
您可以通过以下几种方法之一解决此问题.例如,您可以
You can solve this one of several ways. For instance, you could
- 跟踪读取的行数,并且只读取到该点为止
- 在评估每个条目之前检查每个条目是否为
null
- 使用动态大小的数据结构代替数组,例如
ArrayList
- 对已知值(例如
if (name.equalsIgnoreCase(directory[i]))
)执行检查 - 等
- keep track of the number of lines you read, and only read up to that point
- check each entry to see if it is
null
before evaluating it - use a dynamically sized data structure instead of an array, such as
ArrayList
- perform the check on the known value (e.g.
if (name.equalsIgnoreCase(directory[i]))
) - etc.
这篇关于搜索文本文件时抛出`NullPointerException`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!