为什么当 Clojure 代码调用 JPanel 函数时会收到 nullPointerException?

发布于 2024-12-14 06:49:16 字数 2189 浏览 1 评论 0原文

我无法确定为什么我的代码会出现 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

我感觉我真的错过了一些东西 em> 显而易见,我将不胜感激任何帮助。

谢谢!

I am having trouble determining why my code gets a nullPointer exception. I'm trying to write a swing application using a combination of Java and Clojure. I have a Clojure function that calls a function in my JPanel subclass, and I'm getting a lot of exceptions that are driving me crazy.

I've simplified my code as much as possible, and I'm still getting these errors.

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

Here's the entire JPanel subclass, although I think the only relevant part is the drawBlocks function (which eventually will be replacing the blocks array):

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");
}

}

And the stack trace:

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

I have the feeling that I'm missing something really obvious, and I'd appreciate any help.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空袭的梦i 2024-12-21 06:49:16

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

How about the code that invokes draw-state -- is it possible that's passing in nulls for display?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文