2016-08-15 7 views
0

UPDATE:NVM IM AN IDIOTはうーんNULL

を渡すので、私は最近のパネルについて学んだと私は三目並べプロジェクト、主要な何のスタートメニューを再設計しようとしています。

これが私の夢です:Java対応JPanelの設計上の問題

enter image description here

これは私の現在のコードです:

public static void modeGUI() { 

    //MAIN JFRAME GUI 
    JFrame gui = new JFrame("TicTacToe"); 
    gui.setVisible(true); 
    gui.setSize(600, 200); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //MAIN PANEL 
    JPanel mainPanel = new JPanel(); 
    mainPanel.setLayout(new GridLayout(3, 1)); 
    gui.add(mainPanel); 

    //QUESTION PANEL 
    JPanel questionPanel = new JPanel(); 
    questionPanel.setLayout(new FlowLayout()); 
    JLabel q1 = new JLabel("Please select your mode"); 
    q1.setFont(new Font("Times New Roman", Font.PLAIN, 20)); 
    questionPanel.add(q1); 
    mainPanel.add(questionPanel); 

    //MODE PANEL 
    JPanel modePanel = new JPanel(); 
    modePanel.setLayout(new GridLayout()); 
    JButton[] modeButtons = new JButton[3]; 
    modeButtons[0].setText("First turn"); 
    modeButtons[1].setText("Second turn"); 
    modeButtons[2].setText("P vs. P"); 
    for (JButton modeButton : modeButtons) { 
     modeButton.setFont(new Font("Times New Roman", Font.PLAIN, 20)); 
     modeButton.addActionListener(new Response()); 
     modePanel.add(modeButton); 
    } 
    mainPanel.add(modePanel); 

    //OPTION PANEL 
    JPanel optionsPanel = new JPanel(); 
    optionsPanel.setLayout(new FlowLayout()); 
    mainPanel.add(optionsPanel); 
} 

私はそれを実行しようとすると、しかし、それはこのラインに相関する、30行でエラーが発生します:modeButtons[0].setText("First turn");。誰が間違っているのを見るのを助けることができる? Ttt.main(Ttt.java:7)でTtt.modeGUI(Ttt.java:30) の "メイン" スレッドの例外java.lang.NullPointerExceptionが

+0

_私はそれを実行しようとすると、30行目にエラーが表示されます。どのエラー?してください、完全なスタックトレースを投稿する、それは多くを助ける – BackSlash

+0

@BackSlash申し訳ありません、そして、ありがとう、提案はここにあります:例外 "main" java.lang.NullPointerException \t at Ttt.modeGUI(Ttt.java:30) \t at Ttt.main(Ttt.java:7) – Archie

答えて

1



これはところでエラーであります

ボタンの配列が空です。

JButton[] modeButtons = new JButton[3]; 

この行はJButtonの配列を作成しますが、各要素はデフォルトでnullに設定されています(すべての配列の作成と同様)。

は、だから、modeButtons[0]nullあるので、この

modeButtons[0].setText("First turn"); 

NullPointerExceptionがスローされしようとしたとき。値の割り当てを開始する前に、各ボタンを明示的に定義する必要があります。