NullPointerException:错误在哪里?

发布于 2024-12-20 04:00:34 字数 3863 浏览 0 评论 0原文

您好,我必须解决该程序中的一个问题。我不知道为什么会收到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷清清 2024-12-27 04:00:34

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

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.

煮茶煮酒煮时光 2024-12-27 04:00:34

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

 Person p = personList.get(cod_p);

如果该条目不在 HashTable< 中,则这将是 null /code> (顺便说一句,您应该使用 HashMap)。您不检查这一点,然后尝试使用 p 这将引发异常。

Putting on my ESP hat I'm going to say that in loadNumbers():

 Person p = personList.get(cod_p);

This is going to be null if that entry isn't in the HashTable (BTW, you should be using HashMap). You don't check for that, then you try and use p which is going to throw the exception.

爺獨霸怡葒院 2024-12-27 04:00:34

peopleList(是 Map 而不是 List)的键是 Integer 类型,这意味着键必须是 Integer 才能查找任何内容。你正在查找一个它永远找不到的字符串。

尝试

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

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

Person p = personList.get(Integer.parseInt(cod_p));
if (p == null) throw new IllegalStateException("Unable to find "+cod_p);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文