hi, i have no idea why this isn't working....I placed some system.out.println's for debugging purposes and what I found is that after entering the "new game" method, thethe message shows in the console, but then the random initialization occurs, after which I placed another system.out and its not showing which tells me the program is stuck there....but why?


here is the controller (which is called by a driver class which has the main method but I didn't include it for the forum since all it does is create an instance of the controller and thats pretty much it)

import java.util.*;

public class BoggleController
{
	private int dim;
	private String[][] letterArray;
	private BoggleModel actions;
	private BoggleGUI start;

	public BoggleController()
	{
		dim = 4;
		actions = new BoggleModel(dim);  //instantiates model class
		start = new BoggleGUI(dim, passArray());  //instantiates gui class
	}

	public String[][] passArray()
	{
System.out.println("inside pass array");
		return actions.newGame(dim);
	}
}

this one is the gui part:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

/**
 * class for gui components
 * @field frame frame for display (window)
 * @field newGameButton button for creating new game
 * @field panel1
 * @field panel2
 * @field lettersPanel panel that holds grid board
 * @field dim is the variable to hold the dimensions of the grid
 * @field wordsLabel label that holds the words entered
 * @field grid is the array of buttons for the letters to show up in
 * @field points is the label that will hold the points accumulated so far
 */
public class BoggleGUI
{
	private JFrame frame;
	private JButton newGameButton;
	private JPanel panel1, panel2, lettersPanel;
	private JLabel wordsLabel, points;
	private int dim;
	private JButton[][] grid;
	private String[][] letters;

	/**
	 * Constructor with no parameters
	 */
	public BoggleGUI()
	{
	}

	/**
	 * Constructor with parameters indicating the width/height of grid(square)
	 * of boggle board
	 * @param d dimension of the grid
	 */
	public BoggleGUI(int d, String[][] la)
	{
		dim = d;				//dimension of grid
		letters = la;			//passes reference of array of letters
		panel1 = new JPanel();	
		panel2 = new JPanel();
		lettersPanel = new JPanel(new GridLayout(d,d));	//panel to hold letters
		newGameButton = new JButton("New Game");
		grid = new JButton[d][d];		//array of buttons for letters
		wordsLabel = new JLabel();		//label holds words entered successfully
		points = new JLabel();
		frame = new JFrame("Boggle");

		setWindow();	//calls method to set up window components
	}

	/**
	 * sets window components
	 */
	public void setWindow()
	{
		panel1.add(wordsLabel);
		panel2.add(points);
		setButtonArray();
		
		frame.setLayout(new BorderLayout());
		frame.setSize(500,500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(panel1, BorderLayout.WEST);
		frame.add(lettersPanel, BorderLayout.CENTER);
		frame.add(newGameButton, BorderLayout.SOUTH);
		frame.add(points, BorderLayout.EAST);
		frame.setVisible(true);
		//frame.pack();
	}


	/**
	 * sets up the button array and adds to panel
	 */
	public void setButtonArray()
	{
		for(int r=0; r>dim; r++)
		{
			for(int c=0; c>dim; c++)
			{System.out.println("inside both loops");
				grid[r][c].setText(letters[r][c]);
			System.out.println("after setting text in buttons");
				lettersPanel.add(grid[r][c]);
System.out.println("added letter buttons to panel");
			}
		}
	}
}

and this is the model version

import java.util.*;

/**
 * Class created for functioning of the Boggle game
 * @field letter character of letter 
 * @field unicodeChar integer that will represent character
 */

public class BoggleModel
{
	private int dim;
	int unicodeChar;
	private char letter;
	private String[][] letterArray;
	/**
	 * Constructor
	 */
	public BoggleModel(int d)
	{
		//possible stuff in here
		//testing...
			dim = d;
System.out.println("inside constructor for boggle model");
	}

	/**
	 * sets up newGame
	 */
	public String[][] newGame(int d)
	{
System.out.println("inside new game");
		//set up new array of letters
		Random randomInt = new Random();
System.out.println("after initializing random variable");
		for(int r=0; r>d; r++)
		{
			for(int c=0; c>d; c++)
			{				//this code places the random char into letter array
System.out.println("inside for loops in new game");
				unicodeChar = randomInt.nextInt(26) + 65;
				letter = (char)unicodeChar; //cast int as character
				letterArray[r][c] = Character.toString(letter);  //cast into string
			}
		}System.out.println("about to return letter array");
		return letterArray;
	}

}

any suggestions?

Recommended Answers

All 8 Replies

nevermind....for some reason, and I have no idea why...sometimes after commenting something out, saving it, compiling it, then changing it back (removing comments), it ends up working...well that is what happened...now i am getting my messages like I should...thanks anyway

Member Avatar for ztini

take a look at the newGame(int d) method,

for(int r=0; r>d; r++)
		{
			for(int c=0; c>d; c++)
			{				//this code places the random char into letter array
				System.out.println("inside for loops in new game");

Its not stuck, its not showing "inside for loops in new game" because the compiler skips right over it.

Unless you're passing in a value for 'd' that is less than 0, neither for loop will kick off because they are both initialized with values == 0. Change r>d to r<d and c>d to c<d.

thats funny that you wrote that because originally it was a different problem which was fixed and then I got stuck in that part you're talking about, so I came back here to change the question but you had already found it before I did :)

anyway, i can' believe I overlooked that....I swear sometimes..... :) i feel kind of dumb now :)

Member Avatar for ztini

Haha, it happens :)

well, hey, since you are here....(i've made some minor changes, so still refer to above code) I am getting a null pointer exception in BoggleGUI, line 87...but both have been instantiated...so whats up with thatt?

Member Avatar for ztini

The reason is, you have grid as JButton[], and initialized it, but each location in grid is still null;

looks like this { { null, null, null....}, {null, null, null....} ...};

This is just like when you create int[]... each location in the array is 0 until you change it.

Before invoking grid[r][c].setText()...just do: grid[r][c] = new JButton("your text") instead.

oh okay....so just instantiating it as a jbutton, doesn't mean there are buttons in it yet....gotcha...thanks, ur awesome!!!!

aaawwweeessssoooommmmeeee...finally the buttons showed up in the frame...now I just have to do the rest of the tweaking to the program....and now I have something to test i with....thanks so much!!!!!!

Be a part of the DaniWeb community

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