2011-12-07 33 views
-2

嗨,我必須解決在這個程序中的問題。我不知道爲什麼我收到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) 
+7

如果你問我有點吃不消了代碼,你應該已經把範圍縮小一點。什麼是你得到的空指針異常的棧跟蹤? – codeling

+3

stacktrace提供了很多有用的信息,包括髮生錯誤的那一行。發佈堆棧跟蹤。 – kba

+0

請打印堆棧跟蹤輸出並告訴我們錯誤行在哪裏! –

回答

3

personsList(這是一個Map不是列表)的鍵是Integer類型,這意味着鍵必須是一個整數才能找到任何東西。你正在查找一個它永遠不會找到的字符串。

嘗試

Person p = personList.get(Integer.parseInt(cod_p)); 
if (p == null) throw new IllegalStateException("Unable to find "+cod_p); 
+0

謝謝......謝謝......你是天才...... !!!最後!!! – Mazzy

11

使用調試器,在有問題的代碼的開頭設置一個斷點,然後通過你的代碼一步一步來。調試器是你最好的朋友。

+0

+1:它是一個恥辱,你不能兩次投票。 –

+0

我完全同意。當我看到Eclipse調試工具時,我的生活徹底改變了...... –

4

把我的帽子ESP我要說的是,在loadNumbers()

Person p = personList.get(cod_p); 

這將是null如果該條目不在HashTable(順便說一句,你應該用HashMap) 。你不檢查,然後你嘗試使用p這將拋出異常。

+0

我通過Debubber檢查過,並且獲得了相同的結果。我必須明白爲什麼 – Mazzy

+1

需要:「您應該使用HashMap。」真正。並且要更好,你應該替換「private Hashtable personList;」 「private Map personList;」。這將允許你改變你的hashMap(pr HashTable或衍生物)的實現類型,而無需在其他地方更改代碼。 –

+0

@Mazzy,你正在查找一把鑰匙,你還沒有加載到地圖中。在調試器中,您將能夠看到哪些鍵被加載以及您正在查找哪個鍵。 –