2次元配列を使用し、配列位置から読み取る文字に基づいて現在の座標に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はどれ? –
globalmapがnullでないことを確認してください。 –
真剣に、ライン64は何ですか?私たちはあなたのモニターを見ることができないことを知っていますよね? –