Getting NullPointerException from Hashtable while using GObject method

♀尐吖头ヾ 提交于 2020-01-21 15:21:07

问题


So I try to create a small Zombie-Shooter game. I use a GTurtle class from ACM package (jtf.acm.org). I have additional thread for a GTurtle, which is a GObject. I have a run method with while loop, that is checking if boolean is true, if it is - this.forward() method gets executed.

I tried running game and pressing button, if it is W or D, boolean in GTurtle object gets changed and Thread executes action. Then I get this exception:

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)

回答1:


Judging by the source code for Hashtable.put you either passed key parameter with null or value parameter with null or both null.

From Javadoc.

Throws:

        NullPointerException - if the key or value is null

Note: I do not know the version of the JDK you are using (link below does not have a line 394 matching with your version), although the reasoning remains valid!

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


来源:https://stackoverflow.com/questions/5544375/getting-nullpointerexception-from-hashtable-while-using-gobject-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!