使用GObject方法时从Hashtable获取NullPointerException [英] Getting NullPointerException from Hashtable while using GObject method

查看:133
本文介绍了使用GObject方法时从Hashtable获取NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试创建一个小的Zombie-Shooter游戏.我使用ACM包(jtf.acm.org)中的GTurtle类.我还有一个用于GTurtle的附加线程,它是一个GObject.我有一个带有while循环的run方法,该方法检查boolean是否为true,如果是,则执行this.forward()方法.

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.

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

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)

推荐答案

根据Hashtable.put的源代码判断,您可以将key参数与null一起传递给value或将value参数与null一起传递,或者将两者都传递给.

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

来自 Javadoc .

投掷:

        NullPointerException-如果键或值是null

Throws:

        NullPointerException - if the key or value is null

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

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

这篇关于使用GObject方法时从Hashtable获取NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆