簡體   English   中英

使用GObject方法時從Hashtable獲取NullPointerException

[英]Getting NullPointerException from Hashtable while using GObject method

因此,我嘗試創建一個小的Zombie-Shooter游戲。 我使用ACM包(jtf.acm.org)中的GTurtle類。 我還有一個用於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)

Hashtable.put的源代碼來看,您傳遞的key參數為nullvalue參數為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM