简体   繁体   中英

Getting a nullpointerexception error when trying to draw to canvas

I'm trying to paint essentially a checkerboard pattern to the screen by using a 2d array and painting 10x10 pixel blocks at the current coordinate based on what character is read from the array location. I think this is all the code related to the problem:

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

This is the map array:

0000
0011
0100
0000

Stack trace:

    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)

Line 64:

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

Likely your globalmap array elements are null, but it's hard to tell based on what you're posting. Note that this line is terrible:

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

You need to spread this out on several lines if only for debugging purposes:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM