0

I am trying to understand why I get the following error:

Exception in thread "main" java.lang.NullPointerException
    at HashMapClass.person_exists(HashMapClass.java:12)
    at MainClass.main(MainClass.java:7)

I would have expected that by using contains key I would avoid pointing to nothing. Put another way, how do I query by hashMapClass when it is Empty?

MainClass.java

public class MainClass {

    public static void main(String[] args) {

    HashMapClass people = new HashMapClass();
    boolean e = people.person_exists("foo");
    }
}

HashMapClass.java

import java.util.HashMap;

public class HashMapClass {

    private HashMap<String, PersonClass> people;

    HashMapClass() {
    this.people = people;
    }

    public boolean person_exists(String name) {
    if (this.people.containsKey(name)) {
        return true;}
    else {return false;}}
}

PersonClass.java

import java.util.HashMap;

public class PersonClass {

    private String name;
    private HashMap<String, Integer> archive;
    private int x;

    PersonClass(String name) {
    this.name = name;
    this.archive = archive;
    this.x = 10;
    }
}
beoliver
  • 5,579
  • 5
  • 36
  • 72

0 Answers0