嗨,我必須解決在這個程序中的問題。我不知道爲什麼我收到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
}
當我在主要調用加載方法時,我獲得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)
如果你問我有點吃不消了代碼,你應該已經把範圍縮小一點。什麼是你得到的空指針異常的棧跟蹤? – codeling
stacktrace提供了很多有用的信息,包括髮生錯誤的那一行。發佈堆棧跟蹤。 – kba
請打印堆棧跟蹤輸出並告訴我們錯誤行在哪裏! –