Hi I am new to Java and this is my issue. I can compile my code successfully but when I try to get the program to run it gives me an error stating....
Exception in thread "main" java.lang.NullPointerException
at TictacToe.makeTicTacToe(TictacToe.java:37)
at TictacToe.<init>(TictacToe.java:25)
at TicTacToeMain.main(TicTacToeMain.java:13)
Press any key to continue . . .
I tried everything an I am still lost.. Please give a detail answer... I am new to java and the book is not helping...
import java.awt.Color;
import javax.swing.*;
import javax.swing.*;
import java.awt.*;
public class TictacToe extends JPanel
{
JPanel tictactoe[] = new JPanel[9];
public static int player;
public static String player1 = "";
public static int player1num;
public static String player2 = "";
public static int player2num;
public TictacToe()
{
JLabel jl = new JLabel("TicTacToe", JLabel.CENTER);
jl.setFont(new Font("ariel", Font.BOLD, 20));
this.add(makeTicTacToe(), BorderLayout.CENTER);
this.add(jl, BorderLayout.NORTH);
}
private JPanel makeTicTacToe()
{
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3,3));
for(int i = 0; i < 9; i++)
{
tictactoe[i].setBorder(BorderFactory.createLineBorder(Color.black));
p1.add(tictactoe[i]);
}
JPanel p2 = new JPanel(new BorderLayout());
p2.add(new JLabel("Player 1"),
BorderLayout.WEST);
p2.add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.EAST);
add(new JLabel("Player 2"), BorderLayout.EAST);
add(new JLabel("Whose turn?", JLabel.CENTER), BorderLayout.SOUTH);
return p2;
}
public static void main(String[] args)
{
player1 = JOptionPane.showInputDialog("Player 1, choose 'X' or 'O'");
if(player1.equals("X") || player1.equals("x"))
{
player2 = "O";
player1num = 1;
player2num = 2;
}
else
{
player2 = "X";
player2num = 1;
player1num = 2;
}
player = player1num;
System.out.println("Player1: " + player1 + " Player 2: " + player2);
}
}