The problem i am faced with is getting the program to start up, and i am new to GUI's. so if anyone could tell me why it will not start up or may be the cause of it would be appreciated greatly.
The code is a little sloppy because i have messed around with it so much, and tried fixing the problem myself. but i have run out of ideas and if anyone could tell what is wrong with it so i can fix it would again be greatly appreciated. (i am not asking you to write the code for me i just want to know where the problem is located, since i am new to GUI's.)
The first class is called TTT
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TTT extends JFrame{
//0 = blank, 1 = x, 2 = o
JFrame frame;
JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, start, computerAI, replay, exit;
JLabel title, label;
JOptionPane pane;
int count =0;
int[][] game;
int player;
boolean gameWin;
Icon xImage;
Icon oImage;
String plyone = "Player 1 Wins";
String plytwo = "Player 2 Wins";
String cat = "CATS GAME";
public TTT(){
super("Tic Tac Toe");
//images for x and o
//xImage = new ImageIcon(/*getClass().getResource()*/ "");
gameWin = false;
frame.setLayout(new GridLayout(3,3));
add(start);
add(computerAI);
frame = new JFrame("Tic Tac Toe");
button1 = new JButton();
button1.setPreferredSize(new Dimension(75, 75));
frame.add(button1);
button2 = new JButton();
button2.setPreferredSize(new Dimension(75, 75));
frame.add(button2);
button3 = new JButton();
button3.setPreferredSize(new Dimension(75, 75));
frame.add(button3);
button4 = new JButton();
button4.setPreferredSize(new Dimension(75, 75));
frame.add(button4);
button5 = new JButton();
button5.setPreferredSize(new Dimension(75, 75));
frame.add(button5);
button6 = new JButton();
button6.setPreferredSize(new Dimension(75, 75));
frame.add(button6);
button7 = new JButton();
button7.setPreferredSize(new Dimension(75, 75));
frame.add(button7);
button8 = new JButton();
button8.setPreferredSize(new Dimension(75, 75));
frame.add(button8);
button9 = new JButton();
button9.setPreferredSize(new Dimension(75, 75));
frame.add(button9);
//if player 1 click: button 1 is clicked make Array[0][0] = 1. if a row is all 1 make game win = true
game = new int[2][2];
for(int i = 0;i < game.length;i++){
for(int j = 0;j < game[i].length;j++){
game[i][j] = 0;
}
}
}
public void actionPerformed(ActionEvent e){
count++;
//Player turns
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
player = 1;
}
if(count == 2 || count == 4 || count == 6 || count == 8){
player = 2;
}
else if(count == 10){
pane.showMessageDialog(null, cat);
}
//button pushes
if(e.getSource() == button1 && player == 1){
game[0][0] = 1;
button1.setIcon(xImage);
button1.setEnabled(false);
}
if(e.getSource() == button2 && player == 1){
game[0][1] = 1;
button2.setIcon(xImage);
button2.setEnabled(false);
}
if(e.getSource() == button3 && player == 1){
game[0][2] = 1;
button3.setIcon(xImage);
button3.setEnabled(false);
}
if(e.getSource() == button4 && player == 1){
game[1][0] = 1;
button4.setIcon(xImage);
button4.setEnabled(false);
}
if(e.getSource() == button5 && player == 1){
game[1][1] = 1;
button5.setIcon(xImage);
button5.setEnabled(false);
}
if(e.getSource() == button6 && player == 1){
game[1][2] = 1;
button6.setIcon(xImage);
button6.setEnabled(false);
}
if(e.getSource() == button7 && player == 1){
game[2][0] = 1;
button7.setIcon(xImage);
button7.setEnabled(false);
}
if(e.getSource() == button8 && player == 1){
game[2][1] = 1;
button8.setIcon(xImage);
button8.setEnabled(false);
}
if(e.getSource() == button9 && player == 1){
game[2][2] = 1;
button9.setIcon(xImage);
button9.setEnabled(false);
}
//__________________________________________
if(e.getSource() == button1 && player == 2){
game[0][0] = 2;
button1.setIcon(oImage);
button1.setEnabled(false);
}
if(e.getSource() == button2 && player == 2){
game[0][1] = 2;
button2.setIcon(oImage);
button2.setEnabled(false);
}
if(e.getSource() == button3 && player == 2){
game[0][2] = 2;
button3.setIcon(oImage);
button3.setEnabled(false);
}
if(e.getSource() == button4 && player == 2){
game[1][0] = 2;
button4.setIcon(oImage);
button4.setEnabled(false);
}
if(e.getSource() == button5 && player == 2){
game[1][1] = 2;
button5.setIcon(oImage);
button5.setEnabled(false);
}
if(e.getSource() == button6 && player == 2){
game[1][2] = 2;
button6.setIcon(oImage);
button6.setEnabled(false);
}
if(e.getSource() == button7 && player == 2){
game[2][0] = 2;
button7.setIcon(oImage);
button7.setEnabled(false);
}
if(e.getSource() == button8 && player == 2){
game[2][1] = 2;
button8.setIcon(oImage);
button8.setEnabled(false);
}
if(e.getSource() == button9 && player == 2){
game[2][2] = 2;
button9.setIcon(oImage);
button9.setEnabled(false);
}
// the winning spaces for player 1
if(game[0][0] == 1 && game[0][1] == 1 && game[0][2] == 1){
gameWin = true;
//set button editable false
}
if(game[1][0] == 1 && game[1][1] == 1 && game[1][2] == 1){
gameWin = true;
}
if(game[2][0] == 1 && game[2][1] == 1 && game[2][2] == 1){
gameWin = true;
}
if(game[0][0] == 1 && game[1][0] == 1 && game[2][0] == 1){
gameWin = true;
}
if(game[0][1] == 1 && game[1][1] == 1 && game[2][1] == 1){
gameWin = true;
}
if(game[0][2] == 1 && game[1][2] == 1 && game[2][2] == 1){
gameWin = true;
}
if(game[0][0] == 1 && game[1][1] == 1 && game[2][2] == 1){
gameWin = true;
}
if(game[0][2] == 1 && game[1][1] == 1 && game[2][0] == 1){
gameWin = true;
}
// the winning spaces for player 2
if(game[0][0] == 2 && game[0][1] == 2 && game[0][2] == 2){
gameWin = true;
}
if(game[1][0] == 2 && game[1][1] == 2 && game[1][2] == 2){
gameWin = true;
}
if(game[2][0] == 2 && game[2][1] == 2 && game[2][2] == 2){
gameWin = true;
}
if(game[0][0] == 2 && game[1][0] == 2 && game[2][0] == 2){
gameWin = true;
}
if(game[0][1] == 2 && game[1][1] == 2 && game[2][1] == 2){
gameWin = true;
}
if(game[0][2] == 2 && game[1][2] == 2 && game[2][2] == 2){
gameWin = true;
}
if(game[0][0] == 2 && game[1][1] == 2 && game[2][2] == 2){
gameWin = true;
}
if(game[0][2] == 2 && game[1][1] == 2 && game[2][0] == 2){
gameWin = true;
}
//win or lose?
if(gameWin = true && count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
JOptionPane.showInputDialog(replay, plyone, "Play Again?", DISPOSE_ON_CLOSE);
}
if(gameWin = true && count == 2 || count == 4 || count == 6 || count == 8){
JOptionPane.showInputDialog(replay, plytwo, "Play Again?", DISPOSE_ON_CLOSE);
//JOptionPane with play again? or play with computer AI? Select Level of AI? restart game?
}
//Player AI Chosen from menu
//...
}
}
]
then here is my Main class
[import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args){
TTT ttt = new TTT();
ttt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ttt.setSize(350, 350);
ttt.setVisible(true);
}
}
The Error the compiler gives me when i try to execute my program
Exception in thread "main" java.lang.NullPointerException
at TTT.<init>(TTT.java:28)
at Main.main(Main.java:9)
What the program is supposed to do is make 9 buttons, whenever a button is pushed it will change the value of the 2 dimensional array. as shown in the many if statements above the code will know when there is 3 in a row. once there is 3 in a row the game will set gameWin to true, and check the count, and if the count is 1,3,5,7,9 player1 will win and it will display the appropriate message for player1, if count is 2,4,6,8 then the same thing will happen with player2 instead of player one. if count gets to 10 it will be a cats game and then display a JOptionPane asking if you would like to play again.
Thank you.
This post has been edited by no2pencil: 10 June 2012 - 07:51 PM
Reason for edit:: Added code tags

New Topic/Question
Reply



MultiQuote





|