NullPointerException:错误在哪里?
您好,我必须解决该程序中的一个问题。我不知道为什么会收到 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 。为什么?
这是堆栈跟踪:
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)
Hi I have to resolve an issue in this program. I don't know why I receive the NullPointerException
. The program has to read a text file.
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
}
When I call in the main the load method I obtain NullPointerException
. Why?
Here is the 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用调试器,在有问题的代码的开头设置断点,然后逐步检查代码。调试器是你最好的朋友。
Use a debugger, set a breakpoint at the beginning of the problematic code and then go through your code step by step. The debugger is your best friend.
戴上我的 ESP 帽子,我会在
loadNumbers()
中说:如果该条目不在
HashTable< 中,则这将是
null
/code> (顺便说一句,您应该使用HashMap
)。您不检查这一点,然后尝试使用p
这将引发异常。Putting on my ESP hat I'm going to say that in
loadNumbers()
:This is going to be
null
if that entry isn't in theHashTable
(BTW, you should be usingHashMap
). You don't check for that, then you try and usep
which is going to throw the exception.peopleList(是 Map 而不是 List)的键是 Integer 类型,这意味着键必须是 Integer 才能查找任何内容。你正在查找一个它永远找不到的字符串。
尝试
The key of personsList (which is a Map not a List) is of type
Integer
which means the key has to be an Integer to find anything. You are looking up a String which it will never find.try