我在PhoneDirectory程序中遇到错误。当我尝试编译ArrayPhoneDirectoryTester时,它会成功构建,但是返回错误:
Exception in thread "main" java.lang.NullPointerException
at ArrayPhoneDirectory.addChangeEntry(ArrayPhoneDirectory.java:60)
at ArrayPhoneDirectoryTester.main(ArrayPhoneDirectoryTester.java:17)
Java Result: 1
通过一些在线研究,我了解到这意味着我要使用的变量已设置为null,并且该变量出现在第60行的ArrayPhoneDirectory Java文件中以及第17行的ArrayPhoneDirectoryTester Java文件中(都在下面的代码中进行了注释。)。我仍然不确定它指向哪个变量,因此,对解决此异常的任何帮助将不胜感激。
ArrayPhoneDirectory代码:
import java.io.*;
import java.util.*;
public class ArrayPhoneDirectory implements PhoneDirectory {
private static final int INIT_CAPACITY = 100;
private int capacity = INIT_CAPACITY;
//holds telno of directory entries
private int size = 0;
//Array to contain directory entries
private DirectoryEntry[] theDirectory = new DirectoryEntry[capacity];
//Holds name of data file to be read
private String sourceName = null;
/**
* Flag to indicate whether directory was modified since it was last loaded
* or saved.
*/
private boolean modified = false;
// PUBLIC INTERFACE METHODS
public void loadData(String sourceName) {
Scanner scan = new Scanner(sourceName).useDelimiter("\\Z");
while (scan.hasNextLine()) {
String name = scan.nextLine();
String telno = scan.nextLine();
add(name, telno);
}
}
/**
* find method is called, returning the position in the array of the given
* name.
*/
public String lookUpEntry(String name) {
find(name);
return null;
}
/**
* for loop that checks every DirectoryEntry inside theDirectory and then
* checks it against the name and telno given in the parameter, if both are
* equal, the telno is upda ted, else it is added to theDirectory
*
*/
public String addChangeEntry(String name, String telno) {
for (DirectoryEntry x : theDirectory) {
if (x.getName().equals(name)) { //LINE 60
x.setNumber(telno);
return x.getNumber();
} else {
add(name, telno);
}
}
return null;
}
//TO COMPLETE
public String removeEntry(String name) {
return null;
}
/**
* A new PrintWriter object is created, and a for loop is used to print the
* name and number of each DirectoryEntry to the console.
*/
public void save() {
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter("directory.txt", true));
for (DirectoryEntry x : theDirectory) {
pw.write(x.getName());
pw.write(x.getNumber());
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
pw.close();
}
}
//Private helper methods
private void reallocate() {
capacity = capacity * 2;
DirectoryEntry[] newDirectory = new DirectoryEntry[capacity];
System.arraycopy(theDirectory, 0, newDirectory,
0, theDirectory.length);
theDirectory = newDirectory;
}
private void add(String name, String telno) {
if (size >= capacity) {
reallocate();
}
theDirectory[size] = new DirectoryEntry(name, telno);
size = size + 1;
}
private int find(String name) {
int i = 0;
for (DirectoryEntry x : theDirectory) {
if (x.getName().equals(name)) {
return i;
}
i++;
}
return -1;
}
@Override
public String format() {
return null;
}
}
ArrayPhoneDirectoryTester代码:
public class ArrayPhoneDirectoryTester {
public static void main (String[] args)
{
//creates a new PhoneDirectory
PhoneDirectory newdir = new ArrayPhoneDirectory();
newdir.addChangeEntry("Joe Perkins", "999999"); //LINE 17
System.out.println(newdir);
}
}
最佳答案
当您尝试访问为空的对象时(例如,在空对象上调用方法),将引发NullPointerException。在第60行中:
x.getName().equals(name)
您有两个方法调用。为了使该行抛出NPE,“ x”为空,或者“ x.getName()”为空。如果将其重构为两行,您将知道:
String xName = x.getName();
xName.equals(name)
理想情况下,您应该检查它们是否为空:
String xName = x.getName();
if(xName == null) { } // do some error handling here
xName.equals(name) ...
编辑
这是一个例子:
public String addChangeEntry(String name, String telno) {
for (DirectoryEntry x : theDirectory) {
if (x.getName() == null) {
log.error("Found a user with no name!!);
continue; // or throw an exception if this is really bad for you
}
if (x.getName().equals(name)) { //LINE 60
x.setNumber(telno);
return x.getNumber();
} else {
add(name, telno);
}
}
return null;
}