java null指针异常hashMap containsKey

时间:2014-11-04 17:25:28

标签: java nullpointerexception hashmap

我想了解为什么会出现以下错误:

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

我原本期望通过使用包含键,我会避免指向任何东西。换句话说,如何在hashMapClass为空时进行查询?

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;
    }
}

0 个答案:

没有答案