使用GObject方法从Hashtable获取NullPointerException

Xeo*_*eos 2 java netbeans hashtable

所以我尝试创建一个小型Zombie-Shooter游戏.我使用ACM包中的GTurtle类(jtf.acm.org).我有一个GTurtle的额外线程,这是一个GObject.我有一个带有while循环的run方法,即检查boolean是否为true,如果是,则执行this.forward()方法.

我尝试运行游戏并按下按钮,如果它是W或D,GTurtle对象中的布尔值会被更改并且Thread会执行操作.然后我得到这个例外:

java.lang.NullPointerException
         at java.util.Hashtable.put(Hashtable.java:394)
         at acm.util.JTFTools.pause(JTFTools.java)
         at acm.util.Animator.delay(Animator.java)
         at acm.graphics.GTurtle.setLocation(GTurtle.java)
         at acm.graphics.GObject.move(GObject.java)
         at acm.graphics.GTurtle.move(GTurtle.java)
         at acm.graphics.GObject.movePolar(GObject.java)
         at acm.graphics.GTurtle.forward(GTurtle.java)
         at anotherTryJava.Player.run(Player.java:20)
         at java.lang.Thread.run(Thread.java:662)
Run Code Online (Sandbox Code Playgroud)

Jor*_*ira 12

根据Hashtable.put您的源代码判断,使用key参数with nullvalue参数with null或两者null.

来自Javadoc.

抛出:

        NullPointerException - 如果键或值是 null

注意:我不知道您使用的JDK版本(下面的链接没有与您的版本匹配的394行),尽管推理仍然有效!

http://www.docjar.com/html/api/java/util/Hashtable.java.html

public synchronized V put(K key, V value) {
    if (key != null && value != null) {
        [...]
        return result;
    }
    throw new NullPointerException();
}

Hashtable a = ...;
a.put(null, "s"); // NullPointerException
a.put("s", null); // NullPointerException
Run Code Online (Sandbox Code Playgroud)