public class tic_tac_toe extends JFrame {
MyPanel mp=null;
JButton jbs[];
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
tic_tac_toe game=new tic_tac_toe();
}
public tic_tac_toe(){
mp=new MyPanel();
for(int j=0;j<9;j++){
jbs[j]=new JButton();
mp.add(jbs[j]);
}
this.add(mp);
this.setLayout(new GridLayout(3, 3));
this.setSize(150, 150);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("tic_tac_toe");
}
}
class MyPanel extends JPanel{
}
异常信息
Exception in thread "main" java.lang.NullPointerException
at tic_tac_toe.<init>(tic_tac_toe.java:26)
at tic_tac_toe.main(tic_tac_toe.java:21)
------解决方案--------------------
程序第三行:数组元素个数;
JButton jbs[] = new JButton[9];