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);

	}




}

Recommended Answers

It does not give a run time exception to me. It gives me the following output if I provide X as the input:

Player1: X Player 2: O

Jump to Post

All 4 Replies

It does not give a run time exception to me. It gives me the following output if I provide X as the input:

Player1: X Player 2: O

so, we must assume he did not give 'X' as input. even worse, we can assume that the code he is showing us is not the code he was running when he got the exception.
he get's an exception thrown for a line of code somewhere in his instance methods of an object he never instantiates.

too late...

JPanel tictactoe[] = new JPanel[9];

creates an array with 9 empty (null) slots for JPanels
...

for(int i = 0; i < 9; i++) {
   tictactoe[i].setBorder( ...

tries to reference the JPanel in tictactoe but that array element is still null.
You need to initialise (populate) every element of the array with a new JPanel before you can use them

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.