簡體   English   中英

嘗試在畫布上繪制時出現nullpointerexception錯誤

[英]Getting a nullpointerexception error when trying to draw to canvas

我試圖通過使用2d數組在屏幕上繪制棋盤圖案,並根據從數組位置讀取的字符在當前坐標上繪制10x10像素塊。 我認為這是與問題相關的所有代碼:

public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g.fillRect(0, 0, this.getWidth(),this.getHeight());
        for(int x = 0;x<=3;x++){
            for(int y = 0;y<=3;y++){
                                // NPE occurs on this line:
                if (globalmap[x][y] == '1'){g2d.fillRect(10*y, 10*x, 10,10);}
            }
        }
}

這是地圖數組:

0000
0011
0100
0000

堆棧跟蹤:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at com.side.side.GameEngine.paint(GameEngine.java:64)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JLayeredPane.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paintToOffscreen(Unknown Source)
        at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
        at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
        at javax.swing.RepaintManager.paint(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
        at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
        at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
        at java.awt.Container.paint(Unknown Source)
        at java.awt.Window.paint(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
        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)

第64行:

if (globalmap[x][y] == '1') { g2d.fillRect(10*y, 10*x, 10,10); }

您的globalmap數組元素可能為null,但根據您要發布的內容很難區分。 請注意,這行很糟糕:

 if (globalmap[x][y] == '1'){g2d.fillRect(10*y, 10*x, 10,10);}

如果僅出於調試目的,則需要將其分布在幾行上:

if (globalmap[x][y] == '1') {
    g2d.fillRect(10*y, 10*x, 10,10);
}

暫無
暫無

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

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