Jump to content

Having a bit of difficulty with a game...

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Erudito

Erudito

    Newbie

  • Members
  • Pip
  • 2 posts
Hello! I'm Erudito, and this is my first time posting on here, so don't be too cruel! I'm essentially a beginner at Java, and I'm not very good at it, I must admit. Feel free to point out anything I've worded weirdly, or something that's really strange to you...

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. :)

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
You're trying to add uninitialized JButtons to your frame.

buttons[0] = new JButton("...");
buttons[1] = new JButton("...");
...
buttons[n] = new JButton("...");

**Edit:
I think there is a tic-tac-toe tutorial hiding somewhere in the tutorials section.

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
First, I'll explain how to read this Stack Trace:
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)
First, look for the top-most line where your code is being used, and the line directly above that:
at java.awt.Container.add(Container.java:365)

at TicTacToeworking.createGrid(TicTacToeworking.java: 44)
This tells you the exception was thrown in your createGrid method, on line 44, and that the method that threw the exception was Container.add(). All you do is look for the top-most Class name that you wrote, and that line will have the name of the method that called it, the File name (when you've got a big project that's important!), and the line number. The line above that specifies the method that threw the exception, which is important if you call multiple methods in the same line of code! There's more to reading Stack Traces than that, but that's a pretty good starting point to understanding it.

Line 44 is this:
			frame.add(buttons[i]); 
And looking at your code it's clear why this happened. Consider your object property, the buttons array:
	JButton[] buttons = new JButton[9]; 
Remember that in Java an Array is an object, so the new keyword only made a new Array object, not all the objects that inhabit that array! You need to populate the array with new JButton objects before using them. You don't have a constructor in this class, so why not add this code in a new constructor?
    public TicTacToeworking()

    {

        for (int i = 0; i < buttons.length; ++i)

        {

            buttons[i] = new JButton("Button #" + i);

        }

    }
Instead of using the constant 9 in your loops that are dependent on the size of buttons, you should use buttons.length instead. That way if you add more buttons you don't have to change all your code to reflect that!
Wow I changed my sig!

#4
Erudito

Erudito

    Newbie

  • Members
  • Pip
  • 2 posts
I thank the both of you for answering! I've finally gotten my code to run normally! Now, let's see how long it stays that way... Once again, thank you both very much! (Also, ZekeDragon, I understand Stack Traces a bit better now :))




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users