Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6914691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:22:49+00:00 2026-05-27T09:22:49+00:00

Hi I have to resolve an issue in this program. I don’t know why

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:22:49+00:00Added an answer on May 27, 2026 at 9:22 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so I've spent a couple hours trying to resolve this issue and have
I have this issue that I want to resolve. Lets think we have this
To resolve this issue I have created an open source Java Thread Affinity library
Has there been discussion around how to resolve equivalent openids? Meaning, I personally have
I have an executable that is started by a windows service, this program will
Ok, so I am stumped on this issue. I have seen a lot of
I have a silly error that I am unable to resolve. I try to
When I have several conflicts, is there a way to resolve them all by
I am desperate...I'm working all the day on a program but I didn't resolve
I ask because my buddy posted a question earlier on How to resolve issue

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.