2011-12-07 19 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

を試してみてください。あなたが得るnullpointer例外のStacktraceは何ですか? – codeling

+3

stacktraceは、エラーが発生した行を含む多くの有用な情報を提供します。スタックトレースをポストします。 – kba

+0

stacktraceの出力を表示して、エラーラインがどこにあるか教えてください! –

答えて

3

(地図ではないリストである)personsListのキーは、キーが何かを見つけるための整数であることを有することを意味するタイプ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

私はloadNumbers()にと言うつもりです私のESPの帽子の上に置く:

Person p = personList.get(cod_p); 

これは(ところで、あなたはHashMapを使用する必要があります)、そのエントリがHashTableにない場合nullになるだろう。あなたはそれをチェックしていないので、例外をスローしようとしているpを試してみてください。

+0

私はDebubberを通してチェックしました。同じことを取得しました。理由を理解する必要があります – Mazzy

+1

:「あなたはHashMapを使用しています。真実。さらに良くなるためには、 "private Hashtable personList;"を置き換える必要があります。 を「private Map personList;」とします。これにより、他の場所のコードを変更することなく、ハッシュマップの実装タイプ(hashTableまたはデリバティブ)を変更することができます。 –

+0

@Mazzy、あなたはキーを探しています、あなたはマップに読み込まれていません。デバッガでは、どのキーがロードされ、どのキーがルックアップされているかを見ることができます。 –

関連する問題