I've been working on a Tic Tac Toe game for quite a bit. It's a project I'm doing for class. Everything was working quite fine yesterday when I worked on it, but when I arrived to class today, I'm afraid something went terribly wrong. My code compiles just fine, but it won't run:
(Also, as you can tell, I'm not at all done, so it's really quite sloppy at the moment.)
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JComponent ;
import java.awt.Graphics ;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TicTacToeworking implements ActionListener
{
String winStr;
String namStr1;
String namStr2;
boolean doOver ;
boolean click = false;
JButton[] buttons = new JButton[9];
int clicked = 0;
JFrame frame = new JFrame("Tic-Tac-Toe");
public void displayOpeningMessage()
{
JOptionPane.showMessageDialog(null, "Welcome to Tic Tac Toe! I'm assuming you've played this before, but if not, here are the rules (there's actually only one rule):" + "\n" + "Get three of your given letter in a row vertically, horizontally, or diagonally!" + "\n" + "Player One will be X, and Player Two will be O." + "\n" + "Have fun!");
}
public void enterPlayerName()
{
namStr1 = JOptionPane.showInputDialog("Player One, please enter your name: ");
namStr2 = JOptionPane.showInputDialog("Player Two, please enter your name: ");
}
public void createGrid()
{
frame.setLayout(new GridLayout(3, 3));
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int i = 0; i < 9; i++)
{
frame.add(buttons[i]);
buttons[i].addActionListener(this);
}
frame.setVisible(true);
}
public void actionPerformed(ActionEvent a)
{
for(int j = 0; j < 9; j++)
{
if(a.getSource() == buttons[j])
{
clicked++;
}
if(clicked == 1 || clicked == 3 || clicked == 5 || clicked == 7 || clicked == 9)
{
buttons[j].setText("X");
}
if(clicked == 2 || clicked == 4 || clicked == 6 || clicked == 8 || clicked == 10)
{
buttons[j].setText("O");
}
}
}
public void buttonPressed()
{
}
public void winsAndLosses()
{
}
public void displayFinal()
{
JOptionPane.showMessageDialog(null, "Player One Wins!");
}
public boolean doAgain()
{
doOver = false ;
String answerStr = JOptionPane.showInputDialog("Would you like to play again?");
if(doOver = answerStr.toUpperCase().charAt(0) == 'Y')
{
doOver = true ;
}
else
{
JOptionPane.showMessageDialog(null, "Thanks for playing!");
System.out.print("Game finished.");
}
return doOver;
}
public void runTicTacToe()
{
do
{
displayOpeningMessage();
enterPlayerName();
createGrid();
buttonPressed();
winsAndLosses();
displayFinal();
doAgain();
}
while(doOver == true);
}
public static void main(String[] args)
{
TicTacToeworking demo = new TicTacToeworking();
//demo.runTicTacToe();
demo.createGrid();
}
}
These are the error messages I'm receiving:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1041)
at java.awt.Container.add(Container.java:959)
at javax.swing.JFrame.addImpl(JFrame.java:540)
at java.awt.Container.add(Container.java:365)
at TicTacToeworking.createGrid(TicTacToeworking.java:44)
at TicTacToeworking.main(TicTacToeworking.java:123)
The one at line 44 is particularly distressing... Thank you, however, to everyone who attempts to help, it's very kind of you. :)


Sign In
Create Account

Back to top









