[英]Getting NullPointerException from Hashtable while using GObject method
So I try to create a small Zombie-Shooter game. 因此,我尝试创建一个小的Zombie-Shooter游戏。 I use a GTurtle class from ACM package (jtf.acm.org). 我使用ACM包(jtf.acm.org)中的GTurtle类。 I have additional thread for a GTurtle, which is a GObject. 我还有一个用于GTurtle(即GObject)的附加线程。 I have a run method with while loop, that is checking if boolean is true, if it is - this.forward() method gets executed. 我有一个带有while循环的run方法,即检查boolean是否为true,如果是,则执行this.forward()方法。
I tried running game and pressing button, if it is W or D, boolean in GTurtle object gets changed and Thread executes action. 我尝试运行游戏并按下按钮,如果它是W或D,则GTurtle对象中的布尔值会更改,并且Thread执行操作。 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)
Judging by the source code for Hashtable.put
you either passed key
parameter with null
or value
parameter with null
or both null
. 从Hashtable.put
的源代码来看,您传递的key
参数为null
或value
参数为null
或都为null
。
Throws: 抛出:
NullPointerException
- if the key or value isnull
NullPointerException
如果键或值为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! 注意:尽管推理仍然有效,但我不知道您使用的JDK的版本(下面的链接没有与您的版本匹配的394行)!
http://www.docjar.com/html/api/java/util/Hashtable.java.html 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.