2015-04-17 39 views
2

我正在開發一個tic tac腳趾遊戲,並且有一個問題 - 線程中的異常「AWT-EventQueue-0」java.lang.NullPointerException ,當我嘗試按下按鈕時發生。 buttonText更改爲X,但在此之後,我得到了問題。我相信有什麼不對的if語句: Main.java的源代碼:在線程「AWT-EventQueue-0」中的異常java.lang.NullPointerException if語句

package mytictactoegame; 


public class Main { 
public static boolean playerTurn = true; 
public static boolean playerWon = false; 
public static boolean compWon = false; 
public static boolean playing = true; 
public static ticgame board = new ticgame(); 
public static void main(String[] args) { 


    board.setVisible(true); 


} 
public static void checkforwin(){ 
    //147 

    if (board.button1.getText().equals("X")){ 
     if (board.button4.getText().equals("X")){ 
      if (board.button7.getText().equals("X")){ 
       playerWon = true; 
       compWon = false; 
       board.labelWon.setText ("Player X won!"); 
      } 
     } 
    } 
} 
} 

而且ticgame(GUI):

package mytictactoegame; 
import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JButton; 
import java.awt.GridLayout; 
import com.jgoodies.forms.layout.FormLayout; 
import com.jgoodies.forms.layout.ColumnSpec; 
import com.jgoodies.forms.factories.FormFactory; 
import com.jgoodies.forms.layout.RowSpec; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.Color; 
import javax.swing.JLabel; 

public class ticgame extends JFrame { 

    Main main = new Main(); 
/** 
* 
*/ 
public static final long serialVersionUID = 1L; 
public JPanel contentPane; 




/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       ticgame frame = new ticgame(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public ticgame() { 
    setTitle("Tic Tac Toe"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    contentPane = new JPanel(); 
    contentPane.setBackground(new Color(204, 255, 0)); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(new BorderLayout(0, 0)); 

    JPanel panel = new JPanel(); 
    contentPane.add(panel, BorderLayout.CENTER); 
    panel.setLayout(new GridLayout(3, 3, 0, 0)); 

    JButton button1 = new JButton(""); 
    button1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if (button1.getText().equals("")){ 
       if (Main.playerTurn == true){ 
        button1.setText("X"); 
        Main.checkforwin(); 
        Main.playerTurn = false; 
       } else { 
        button1.setText("O"); 
        Main.checkforwin(); 
        Main.playerTurn = true; 
       } 
      } 
     } 
    }); 
    panel.add(button1); 
    JButton button4 = new JButton(""); 
    button4.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if (button4.getText().equals("")){ 
       if (Main.playerTurn == true){ 
        button4.setText("X"); 
        Main.checkforwin(); 
        Main.playerTurn = false; 
       } else { 
        button4.setText("O"); 
        Main.checkforwin(); 
        Main.playerTurn = true; 
       } 
      } 
     } 
    }); 
    panel.add(button4); 
    JButton button7 = new JButton(""); 
    button7.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if (button7.getText().equals("")){ 
       if (Main.playerTurn == true){ 
        button7.setText("X"); 
        Main.checkforwin(); 
        Main.playerTurn = false; 
       } else { 
        button7.setText("O"); 
        Main.checkforwin(); 
        Main.playerTurn = true; 
       } 
      } 
     } 
    }); 
    panel.add(button7); 
    JPanel panel_1 = new JPanel(); 
    contentPane.add(panel_1, BorderLayout.EAST); 
    panel_1.setLayout(new FormLayout(new ColumnSpec[] { 
      FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 
      ColumnSpec.decode("117px"),}, 
     new RowSpec[] { 
      FormFactory.RELATED_GAP_ROWSPEC, 
      RowSpec.decode("29px"), 
      FormFactory.RELATED_GAP_ROWSPEC, 
      FormFactory.DEFAULT_ROWSPEC, 
      FormFactory.RELATED_GAP_ROWSPEC, 
      FormFactory.DEFAULT_ROWSPEC, 
      FormFactory.RELATED_GAP_ROWSPEC, 
      FormFactory.DEFAULT_ROWSPEC, 
      FormFactory.RELATED_GAP_ROWSPEC, 
      FormFactory.DEFAULT_ROWSPEC,})); 

    JButton buttonNewGame = new JButton("New Game"); 
    buttonNewGame.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      button1.setText(""); 
      button2.setText(""); 
      button3.setText(""); 
      button4.setText(""); 
      button5.setText(""); 
      button6.setText(""); 
      button7.setText(""); 
      button8.setText(""); 
      button9.setText(""); 
      Main.playerTurn = true; 
      Main.playerWon = false; 
      Main.compWon = false; 


     } 
    }); 
    buttonNewGame.setForeground(Color.RED); 
    buttonNewGame.setBackground(new Color(153, 255, 0)); 
    panel_1.add(buttonNewGame, "2, 2, left, top"); 

    JButton buttonHistory = new JButton("History"); 
    buttonHistory.setForeground(Color.RED); 
    panel_1.add(buttonHistory, "2, 4"); 

    JButton buttonExit = new JButton("Exit"); 
    buttonExit.setForeground(Color.RED); 
    buttonExit.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      dispose(); 
     } 
    }); 
    panel_1.add(buttonExit, "2, 6"); 

    JLabel labelWon = new JLabel(); 
    labelWon.setText ("Who won?"); 
    labelWon.setForeground(Color.GREEN); 
    panel_1.add(labelWon, "2, 10, left, default"); 
} 
public JButton button1; 
public JButton button2; 
public JButton button3; 
public JButton button4; 
public JButton button5; 
public JButton button6; 
public JButton button7; 
public JButton button8; 
public JButton button9; 
public JLabel labelWon; 

}

這裏的第一線錯誤日誌,其餘不適合由於字數:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at mytictactoegame.Main.checkforwin(Main.java:20) 
at mytictactoegame.ticgame$2.actionPerformed(ticgame.java:71) 
+4

哪一行拋出NPE?請向我們展示您的堆棧跟蹤。您將需要了解如何調試NPE(NullPointerException)的一般概念。 **你應該仔細閱讀你的異常的堆棧跟蹤,找到有故障的代碼行,引發異常的代碼行,然後仔細檢查該行**,找出哪個變量爲空,然後追溯到代碼中以查看爲什麼。你會一次又一次遇到這些,相信我。 –

+0

第20行在哪裏?你初始化了所有的按鈕和標籤嗎? – user2418306

+0

第20行是checkforwin() 你怎麼初始化按鈕?他們工作壽,並沒有錯誤,當我順便啓動程序 –

回答

1

你是陰道你的變量如你的button1變量和其他類似的變量。通過遮蔽,我的意思是你在類中聲明它們,但是重申聲明並在你的類的構造函數中初始化重新聲明的變量。當你這樣做時,該字段(在類中聲明的變量)保持爲空。解決方案:不要通過在構造函數中重新聲明它們來影響變量。

例如,你這樣做:

// should be named TicGame to comply with Java naming standards 
public class ticgame extends JFrame { 

    public ticgame() { 

     // .... 

     // here you re-declare the button1 variable 
     // by doing this, you initialize the local variable that 
     // is present int he constructor but leave the class field null 
     JButton button1 = new JButton(""); 

     //.... 
    } 

    public JButton button1; // this guy remains null 
    // ..... 
} 

當你應該這樣做:

public class TicGame extends JFrame { 

    public TicGame() { 

     // .... 

     // JButton button1 = new JButton(""); 
     button1 = new JButton(""); // note the difference? 

     //.... 
    } 

    public JButton button1; // now he's not null! 
    // ..... 
} 
+0

非常感謝!麻煩解決了 –

+1

@Dmytry恐怕你的麻煩纔開始。你的代碼有很多錯誤。如果你打算做得更好,我建議你[請求某人評論](http://codereview.stackexchange.com/)。 – user2418306

+0

它只是一個計算機科學ISP,所以我不擔心代碼,只要它工作。如果它看起來太完美了,我的老師會懷疑有事情發生,生病得到0 :) –

相關問題