我试图通过使用2d数组绘制基本上棋盘图案到屏幕上,并在当前坐标上绘制10x10像素块(基于从阵列位置读取什么字符) 。我想这是所有相关的问题代码:试图画到画布时得到一个nullpointerexception错误
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); }
......哪条线是线64? – 2011-12-18 15:33:47
你确定globalmap不是null吗? – 2011-12-18 15:37:07
说真的,第64行是什么?你知道我们看不到你的显示器,对吧? – 2011-12-18 15:51:07