您好,我必须解决此计划中的问题。我不知道为什么收到NullPointerException
。程序必须读取文本文件。
public class Phone {
private String phone_number;
private String description;
public Phone(String p_n,String d){
phone_number=p_n;
description=d;
}
//unrelated getters, setters
}
import java.util.*;
public class Person {
private String surname;
private String name;
private String title;
private String mail_addr;
private String company;
private String position;
private Phone homephone;
private Phone officephone;
private Phone cellphone;
private Collection<Phone> otherphonebooklist;
public Person(String surname,String name,String title,String mail_addr,String company,String position){
this.surname=surname;
this.name=name;
this.title=title;
this.mail_addr=mail_addr;
this.company=company;
this.position=position;
otherphonebooklist=new ArrayList<Phone>();
}
//unrelated methods
public Collection<Phone> getOtherPhoneBookList(){
return otherphonebooklist;
}
//unrelated methods
}
import java.util.*;
import java.io.*;
/*
* This class rappresent the object
* list of person
*/
public class PhoneBook {
private Hashtable<Integer,Person> personList;
public PhoneBook(){
personList=new Hashtable<Integer,Person>();
}
public void loadPerson(String path) {
try {
BufferedReader reader = new BufferedReader(new FileReader(path));
String surname=reader.readLine();
while(surname!=null){
String name=reader.readLine();
String title=reader.readLine();
String mail_addr=reader.readLine();
String company=reader.readLine();
String position=reader.readLine();
Integer cod_p=Integer.parseInt(reader.readLine());
Person person = new Person(surname,name,title,mail_addr,company,position);
personList.put(cod_p,person);
surname=reader.readLine();
}
}
catch(FileNotFoundException ffe){
System.err.println("Error: the person file does not exist");
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
private void loadNumbers(String numbers){
try {
BufferedReader reader= new BufferedReader(new FileReader(numbers));
String cod_p=reader.readLine();
while(cod_p!=null){
String description=reader.readLine();
String num=reader.readLine();
Phone phone_number=new Phone(num,description);
Person p = personList.get(cod_p);
if(description.equalsIgnoreCase("home phone"))
p.setHomePhone(phone_number);
else if(description.equalsIgnoreCase("office phonne"))
p.setOfficePhone(phone_number);
else if(description.equalsIgnoreCase("cell phone"))
p.setCellPhone(phone_number);
else
p.getOtherPhoneBookList().add(phone_number);
cod_p=reader.readLine();
}
}
catch(FileNotFoundException ffe){
System.err.println("Error: the number file does not exist!");
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
public void load(String p1,String p2){
loadPerson(p1);
loadNumbers(p2);
}
//unrelated methods
}
当我在main中调用load方法时,我获得了NullPointerException
。为什么呢?
这是stacktrace:
Exception in thread "main" java.lang.NullPointerException
at PhoneBook.loadNumbers(PhoneBook.java:75)
at PhoneBook.load(PhoneBook.java:92)
at ManagementPhoneBook.main(ManagementPhoneBook.java:11)
答案 0 :(得分:11)
使用调试器,在有问题的代码的开头设置断点,然后逐步完成代码。调试器是你最好的朋友。
答案 1 :(得分:4)
戴上我的ESP帽子,我会在loadNumbers()
:
Person p = personList.get(cod_p);
如果该条目不在null
(BTW,您应该使用HashTable
),那么这将是HashMap
。你没有检查,然后你尝试使用p
来抛出异常。
答案 2 :(得分:3)
personsList(不是List而不是List)的键是Integer
类型,这意味着键必须是Integer才能找到任何内容。你正在查找一个它永远找不到的字符串。
试
Person p = personList.get(Integer.parseInt(cod_p));
if (p == null) throw new IllegalStateException("Unable to find "+cod_p);