gpt4 book ai didi

java - 为什么在 Clojure 代码调用 JPanel 函数时会出现 nullPointerException?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:00:13 24 4
gpt4 key购买 nike

我无法确定为什么我的代码会出现 nullPointer 异常。我正在尝试结合使用 Java 和 Clojure 来编写一个 swing 应用程序。我有一个 Clojure 函数调用我的 JPanel 子类中的一个函数,但我遇到了很多让我抓狂的异常。

我已经尽可能地简化了我的代码,但我仍然遇到这些错误。

(defn draw-state
"Draws the current state."
[state display]
(do-swing (.drawBlocks display)))

这是整个 JPanel 子类,尽管我认为唯一相关的部分是 drawBlocks 函数(最终将替换 blocks 数组):

public class TDisplay extends JPanel {


private Block[] blocks = new Block[1];

/**
* Create the panel.
*/
public TDisplay() {
setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
setBackground(Color.WHITE);
setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
blocks[0] = new Block(3, 4, 0);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Block b : blocks) {
if (b == null) continue;

switch (b.colour) {
case 0:
g.setColor(Color.WHITE);
break;
case 1:
g.setColor(Color.BLACK);
break;
}
g.fillRect(b.x * Block.SIZE, b.y * Block.SIZE, Block.SIZE, Block.SIZE);
}
}

public void drawBlocks() {
System.out.println("Hello world");
}

和堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: java.lang.NullPointerException
at clojure.lang.AFn.run(AFn.java:28)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at clojure.lang.Reflector.invokeNoArgInstanceMember(Reflector.java:263)
at core$draw_state$fn__401.invoke(core.clj:43)
at clojure.lang.AFn.run(AFn.java:24)
... 8 more

我觉得我遗漏了一些非常明显的东西,如果有任何帮助,我将不胜感激。

谢谢!

最佳答案

调用绘制状态的代码怎么样——是否有可能传递空值以供显示?

关于java - 为什么在 Clojure 代码调用 JPanel 函数时会出现 nullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8040601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com