###実現したいこと
stone型の二次元配列に値を設定したい(初期化).
###問題点
メンバ変数で宣言するためにBoard
の中でStone[][] stone=new Stone[8][8];
と宣言した。しかし存在しないと言われたが理由がわからない。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "Stone.setObverse(int)" because "this.stone[<local8>][<local9>]" is null at Board.paint(Reversi.java:77) at Reversi.paintComponent(Reversi.java:110) at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074) at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907) at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083) at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907) at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083) at java.desktop/javax.swing.JLayeredPane.paint(JLayeredPane.java:586) at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907) at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5271) at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedFPScales(RepaintManager.java:1707) at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1616) at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1556) at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323) at java.desktop/javax.swing.JComponent.paint(JComponent.java:1060) at java.desktop/java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39) at java.desktop/sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:75) at java.desktop/sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:112) at java.desktop/java.awt.Container.paint(Container.java:2003) at java.desktop/java.awt.Window.paint(Window.java:3949) at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:876) at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848) at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823) at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772) at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884) at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
###コード
java
1import java.awt.*; 2import javax.swing.*; 3 4//石に対応するクラス 5class Stone{ 6 public final static int black=1; //黒 7 public final static int white=2; //白 8 private int obverse; //表面の色 9 //コンストラクタ 10 Stone(){ 11 obverse=0; //非配置にする 12 } 13 14 //表面の色を設定する 15 void setObverse(int color){ 16 if(color==black || color==white){ 17 obverse=color; 18 }else{ 19 System.out.println("黒か白でなければいけません"); 20 } 21 } 22 23 //表面の色で中心p,半径radの円を塗りつぶす 24 void paint(Graphics g,Point p,int rad){ 25 /*省略*/ 26 } 27} 28 29class Board{ 30 Stone[][] stone=new Stone[8][8]; 31 32 //コンストラクタ 33 Board(){ 34 } 35 36 //画面描画 37 void paint(Graphics g,int unit_size){ 38 int rad=32; 39 40 /*省略*/ 41 42 int px=120,py=120; 43 for(int i=0;i<8;i++){ 44 for(int j=0;j<8;j++){ 45 stone[i][j].setObverse(1); 46 } 47 } 48 int b=1,w=2; 49 stone[3][3].setObverse(b); 50 stone[3][4].setObverse(w); 51 stone[4][3].setObverse(w); 52 stone[4][4].setObverse(b); 53 for(int i=0;i<8;i++){ 54 for(int j=0;j<8;j++){ 55 Point p=new Point(); 56 p.x=px; //中心座標を定義 57 p.y=py; 58 stone[i][j].paint(g,p,rad); 59 py=py+80; 60 } 61 px=px+80; 62 } 63 } 64} 65 66 67public class Reversi extends JPanel{ 68 public final static int UNIT_SIZE=80; 69 Board board =new Board(); 70 71 //コンストラクタ 72 public Reversi(){ 73 setPreferredSize(new Dimension(800,800)); 74 } 75 76 //画面描画 77 public void paintComponent(Graphics g){ 78 board.paint(g, UNIT_SIZE); //Boardクラスのpaintメソッドを呼び出す 79 } 80 81 //起動 82 public static void main(String[] args){ 83 JFrame f=new JFrame(); 84 f.getContentPane().setLayout(new FlowLayout()); 85 f.getContentPane().add(new Reversi()); 86 f.pack(); 87 f.setResizable(false); 88 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 89 f.setVisible(true); 90 } 91 92 93}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/11/15 15:00