Java NullPointerException:其中';错在哪里?

Java NullPointerException:其中';错在哪里?,java,collections,Java,Collections,嗨,我必须解决这个程序中的一个问题。我不知道为什么我会收到NullPointerException。程序必须读取一个文本文件 public class Phone { private String phone_number; private String description; public Phone(String p_n,String d){ phone_number=p_n; description=d; } //unrel

嗨,我必须解决这个程序中的一个问题。我不知道为什么我会收到
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

}

使用调试器,在有问题的代码开头设置断点,然后逐步检查代码。调试器是你最好的朋友。

戴上我的ESP帽子,我要在
loadNumbers()中说:

如果该条目不在
哈希表
(顺便说一句,您应该使用
哈希映射
),则该值将为
null
。你不检查它,然后你尝试使用
p
,它将抛出异常。

PersonList的键(它是一个映射而不是一个列表)的类型是
Integer
,这意味着该键必须是一个整数才能找到任何东西。您正在查找一个它永远找不到的字符串

试一试


如果你问我,你应该把它缩小一点。得到的nullpointer异常的Stacktrace是什么?Stacktrace提供了很多有用的信息,包括错误发生在哪一行。张贴stacktrace。请打印stacktrace输出并告诉我们错误行在哪里!你是否无法努力缩小问题的范围?甚至连行号都没有?也许否决这个问题有点苛刻。OP没有经验,需要一个指导。下次她/他将粘贴堆栈跟踪,甚至更好,由她/他自己推断。但在那之前…我通过Debbber检查了一下,得到了同样的结果。我必须理解为什么:“你应该使用HashMap。”没错。更好的是,您应该将“private Hashtable personList;”替换为“private Map personList;”。这将允许您更改hashMap(pr HashTable或派生)的实现类型,而无需在其他任何地方更改代码。@Mazzy,您正在查找一个键,但尚未加载到映射中。在调试器中,您将能够看到加载了哪些键以及正在查找的键。@Mazzy。当然,调试器也有同样的效果,但至少现在您知道错误发生在哪个代码行,并且可以检查变量。如果你做对了,你会看到哪个变量是空的,它在哪里导致了一个例外。当我看到Eclipse调试工具时,我的生活完全改变了…谢谢…谢谢…你是个天才。。。!!!最后
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)
 Person p = personList.get(cod_p);
Person p = personList.get(Integer.parseInt(cod_p));
if (p == null) throw new IllegalStateException("Unable to find "+cod_p);