简体   繁体   English

使用GObject方法时从Hashtable获取NullPointerException

[英]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参数为nullvalue参数为null或都为null

From Javadoc . 来自Javadoc

Throws: 抛出:

NullPointerException - if the key or value is null 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.

相关问题 使用Mockito调用Service类的嵌套方法时获取NullPointerException - Getting NullPointerException while calling nested method of Service class using mockito 使用toString从哈希表获取字符串表示形式 - Getting string representations from a hashtable using a toString 调用get方法时来自java.util.Hashtable的AbstractMethodError - AbstractMethodError from java.util.Hashtable while calling get method 哈希表NullPointerException - Hashtable NullPointerException 在spring中使用jdbcTemplate时获取nullpointerException - Getting nullpointerException while using the jdbcTemplate in spring 从我的JButton获取字体时发生NullPointerException - NullPointerException while getting font from my JButton 从 groovy 脚本调用方法时,我在 Jenkisn 上收到 java.lang.NullPointerException - I am getting java.lang.NullPointerException on Jenkisn while calling method from groovy script 使用Mockito调用模拟方法时发生NullPointerException - NullPointerException while invoking mocked method using Mockito 使用EJB 3在Spring 3.2.6中使用Deprecated AbstractStatelessSessionBean的onEjbCreate方法时获取NullPointerException - Getting NullPointerException while using onEjbCreate method of Deprecated AbstractStatelessSessionBean in Spring 3.2.6 with EJB 3 从图库中选择图片时获取NullPointerException - Getting NullPointerException while picking Picture From Gallery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM