Jump to content

Recommended Posts

Posted

new program new problem. : P

 

this one is particularly frustrating, because so far its almost exactly the same program, but with some variables changes, and methods altered a little bit.

 

its giving me a null pointer exception and i cant figure out why.

 

import java.awt.*;
import java.awt.event.*;
import java.awt.Image.*;

public class sudokuWork extends Frame {

 nineGrid[][] grids= new nineGrid[3][3];
 Image theBuffer;
    Graphics BGraphic;
    boolean newEqu=true;


public sudokuWork() {

    	//set up the menu bar       
       MenuBar menuBar = new MenuBar();
       Menu menuFile = new Menu();
       MenuItem menuFileExit = new MenuItem();
       MenuItem menuFileNew= new MenuItem();

       menuFile.setLabel("File");
       menuFileExit.setLabel("Exit");
       menuFileNew.setLabel("New");

       // Add action listener.for the menu button
       menuFileExit.addActionListener
       (
           new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   sudokuWork.this.windowClosed();
               }
           }
       ); 
       menuFileNew.addActionListener
       (

       	new ActionListener(){
       		public void actionPerformed(ActionEvent e){
       			newEqu=true;
       		}
       	}	

       );

       menuFile.add(menuFileNew);
       menuFile.add(menuFileExit);
       menuBar.add(menuFile);

       setTitle("Sudoku!");
       setMenuBar(menuBar);
       setSize(new Dimension(500, 500));
       setVisible(true);

       // Add window listener.
       this.addWindowListener
       (
           new WindowAdapter() {
               public void windowClosing(WindowEvent e) {
                   sudokuWork.this.windowClosed();
               }
           }
       );  


      theBuffer= createImage(getWidth(),getHeight());

      BGraphic= theBuffer.getGraphics();



}





public void play()
   {

   	initializeGrid();
   	do
   	{
System.out.println("wtf is it doing here?");
   	drawButtons(BGraphic);
   	paint(getGraphics());
   	}while(true);

   }
   public void paint(Graphics g)
   {   

   	if(BGraphic == null)
   	{
   		System.out.println("BGraphic is null");
   	}
   	else
   	{

    	try
   		{
   			g.drawImage(theBuffer,0,0,this);
   		}
   		catch(Exception e)
   		{
   			System.out.println("problem drawing image because" + e.toString());

   		}
   	}
}


private void initializeGrid()
{
	System.out.println("initializing the grid");
	for(int i=0; i<=2;i++)
	{
		for(int p=0; p<=2; p++)
		{
			System.out.println(i + " " + p);
			try
			{
				grids[i][p].initialize(30*i,30*p);
			}
			catch(Exception e)
			{
				System.out.println("problem because" + e.toString());
			}
		}
	}
}

private void drawButtons(Graphics BGraphic)
{
	for(int i=0; i<=2;i++)
	{
		for(int p=0; p<=2; p++)
		{

			grids[i][p].draw(BGraphic);
		}
	}
}

protected void windowClosed() {

   	// TODO: Check if it is save to close the application

       // Exit application.
       System.exit(0);
   }
   public void pause(long milliPause)// leave this alone!
// continues to query the system for the time until the first time - current time 
// exceeds the long passed into the function	
{
	long start = System.currentTimeMillis();
	while(System.currentTimeMillis()-start < milliPause)
	{
	// do nothing	
	}	

}

}

 

the error is occuring on line 122 where it says

 

grids[p].initialize(30*i,30*p);

 

 

(in the initialize grid method near the bottom)

 

if i skip over that it hits the same problem at line 139 where it says

 

grids[p].draw(BGraphic);

 

any help would be appreciated.

Posted

Ok, this may be me being stupid, but where have you actually set the elements in the nineGrid array to some object? You are calling the initialise method on some element of the array but all you have are null values in there at the minute. OK, you have

nineGrid[][] grids= new nineGrid[3][3];

 

at the top but all that does is initialise the array to a particular size etc. For instance if I were to do Integer[][] nums = new Integer[4][4], there wouldn't be any integer objects at each of the array elements, just null references (ie they reference to nothing). So you are trying to call the method initialise, but it needs to be called on an Object, hence why you get the NullPointerException. If you have some class nineGrid then you will need to set it up by setting the elements of the array to a new nineGrid object, preferably taking in the arguments that are being taken into initialise into the constructor instead like so -

 

grids[i][p] = new nineGrid(30*i,30*p);

 

 

P.S - Just as an aside, as far as I know, the standard is to begin all class names with an upper case letter, and all variables with a lower case to avoid confusion - http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html

Posted

Could you show the code for nineGrid?

 

And you are saying you have -

try{ 
   grids[i][p] = new nineGrid();
   grids[i][p].initialize(30*i,30*p);
}...

?

 

Not that this won't be the problem (either way works) but just wondering why you have an initialise method seperate from the constructor, there are reasons one might do this but it seems like it would be easier in this case simply to initialise everything in the constructor.

Posted

i tried it like that and with the first line above the try()

 

 

the reason i have an initialize function is to set the location. each nineGrid is laid out next to the other ones (sudoku).

nineGrids

import java.awt.*;
import java.awt.event.*;
import java.awt.Color.*;
public class nineGrid extends Component{
private int xCoor;
private int yCoor;
private int length=30;
private int width=30;
private box[][] boxes= new box[3][3];




public nineGrid()
{

}


public void draw(Graphics g)
{
	for(int i=0; i<=2; i++)
	{
		for(int p=0; p<=2;p++)
		{
			boxes[i][p].draw(g);	
		} 
	}

}
public void initialize(int a, int b)
{
	yCoor=(10*(width+5))+a;
	xCoor=(10*(length+5))+b;
	for(int i=0; i<=2; i++)
	{
		for(int p=0; p<=2;p++)
		{
			boxes[i][p].initialize(i,p);	
		} 
	}

}


}

 

 

thinking about it now, i probably could do it in the constructor but it doesnt really matter, cause i hit the same problem when it gets to the draw function

Posted

Theres a very good reason you are getting the same error... it's because you are making the same mistake in the nineGrid class. You are not creating any box objects. You need to do the same thing for your boxes as you did for your nineGrids.

Posted

you mean add the =new box() line?

 

all better. if im allowed to be stupid one time im allowed to do it twice, right? : D

 

now i just have to work out how to do input and the logic and all that.

Posted

bleh.

 

progress has certainly been made, but ive hit another snag. as part of my problem solving for why it never fills in any numbers, i set it up to wait for me to hit solve again after each time through all the squares.

 

i discovered that despite the fact that solve() is placed inside an infinite loop (do{}while(true); ) and the fact that it does set solvePressed to true each time, it doesnt ever actually go through the solve function again.

 

any ideas why?

 

import java.awt.*;
import java.awt.event.*;
import java.awt.Image.*;

public class sudokuWork extends Frame {

 nineGrid[][] grids= new nineGrid[3][3];
 Image theBuffer;
    Graphics BGraphic;
    boolean newEqu=true;
    boolean solvePressed=false;

public sudokuWork() {

    	//set up the menu bar       
       MenuBar menuBar = new MenuBar();
       Menu menuFile = new Menu();
       MenuItem menuFileExit = new MenuItem();
       MenuItem menuFileNew= new MenuItem();
       Menu menuSolve=new Menu();
       MenuItem menuSolveSolve=new MenuItem();

       menuFile.setLabel("File");
       menuFileExit.setLabel("Exit");
       menuFileNew.setLabel("New");
       menuSolve.setLabel("Solve");
       menuSolveSolve.setLabel("Solve!");

       menuSolveSolve.addActionListener
       (
       	new ActionListener(){
       		public void actionPerformed(ActionEvent e) {
       			solvePressed=true;
       			System.out.println(solvePressed);
       		}
       	}
       );

       // Add action listener.for the menu button
       menuFileExit.addActionListener
       (
           new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   sudokuWork.this.windowClosed();
               }
           }
       ); 
       menuFileNew.addActionListener
       (

       	new ActionListener(){
       		public void actionPerformed(ActionEvent e){
       			newEqu=true;
       		}
       	}	

       );

       menuFile.add(menuFileNew);
       menuFile.add(menuFileExit);
       menuBar.add(menuFile);
       menuSolve.add(menuSolveSolve);
       menuBar.add(menuSolve);

       setTitle("Sudoku!");
       setMenuBar(menuBar);
       setSize(new Dimension(500, 500));
       setVisible(true);

       // Add window listener.
       this.addWindowListener
       (
           new WindowAdapter() {
               public void windowClosing(WindowEvent e) {
                   sudokuWork.this.windowClosed();
               }
           }
       );  


      theBuffer= createImage(getWidth(),getHeight());

      BGraphic= theBuffer.getGraphics();



}





public void play()
   {

   	initializeGrid();
   	for(int i=0; i<=2; i++)
   	{
   		for(int p=0; p<=2; p++)
   		{
   			for(int m=0; m<=2; m++)
   			{
   				for(int n=0; n<=2; n++)
   				{
   					this.add(grids[i][p].getGrid(m,n));
   				}
   			}
   		}
   	}
   	do
   	{
 		//System.out.println("wtf is it doing here?");
 		if(solvePressed==true)
 		{
 			solve();
 			solvePressed=false;
 		}
    	drawButtons(BGraphic);
    	paint(getGraphics());
   	}while(true);

   }
   public void paint(Graphics g)
   {   

   	if(BGraphic == null)
   	{
   		System.out.println("BGraphic is null");
   	}
   	else
   	{

    	try
   		{
   			g.drawImage(theBuffer,0,0,this);
   		}
   		catch(Exception e)
   		{
   			System.out.println("problem drawing image because" + e.toString());

   		}
   	}
}


private void initializeGrid()
{

	int length=400;
	int width=400;
	System.out.println("initializing the grid");
	for(int i=0; i<=2;i++)
	{
		for(int p=0; p<=2; p++)
		{
			//System.out.println(i + " " + p);
			grids[i][p]= new nineGrid(length,width);
			try
			{

				grids[i][p].initialize(i*(length/3),p*(length/3));
			}
			catch(Exception e)
			{
				System.out.println("problem because" + e.toString());
			}
		}
	}
}
public void solve()
{
	System.out.println("Solvin!");

	//go through each square, starting at top left, moving down and right through that section, then moving down and left through the sections
	for(int i=0; i<=2; i++)
	{
		for(int p=0; p<=2; p++)
		{
			for(int m=0; m<=2; m++)
			{
				for(int n=0; n<=2; n++)
				{
	//////////////////////////////////end of moving//check if that box is known////////////////
					if((grids[i][p].getGrid(m,n)).isKnown())
					{

						int counter=0;
						while(Math.pow(3, counter)!=grids[i][p].getGrid(m,n).getTotal())
						{
							counter++;
						}	
	////////if it is known, go to all its shared rows, columns, and sections, and remove that number/////////
						for(int r=0;r<=2;r++)
						{
							for(int c=0;c<=2;c++)
							{
								//System.out.println("removed:" + counter + "From" + r + "," + p + "," + c +","+n);
								grids[r][p].getGrid(c,n).removeNum(counter);
								grids[i][r].getGrid(m,c).removeNum(counter);
								grids[i][p].getGrid(r,c).removeNum(counter);
								System.out.println(grids[i][p].getGrid(r,c).getTotal());
							}
						}
					}
				}	
			}
		}	
	}
	int numsleft=0;
	for(int i=0; i<=2; i++)
	{
		for(int p=0; p<=2; p++)
		{
			for(int m=0; m<=2; m++)
			{
				for(int n=0; n<=2; n++)
				{
					grids[i][p].getGrid(m,n).checkFound();
				}
			}
			numsleft+=grids[i][p].checkEmpties();
		}
	}
	System.out.println("numsLeft=" +numsleft);
}

private void drawButtons(Graphics BGraphic)
{
	for(int i=0; i<=2;i++)
	{
		for(int p=0; p<=2; p++)
		{

			grids[i][p].draw(BGraphic);
		}
	}
}

protected void windowClosed() {

   	// TODO: Check if it is save to close the application

       // Exit application.
       System.exit(0);
   }
   public void pause(long milliPause)// leave this alone!
// continues to query the system for the time until the first time - current time 
// exceeds the long passed into the function	
{
	long start = System.currentTimeMillis();
	while(System.currentTimeMillis()-start < milliPause)
	{
	// do nothing	
	}	

}

}

Posted

   	do
   	{
 		//System.out.println("wtf is it doing here?");
 		if(solvePressed==true)
 		{
 			solve();
 			solvePressed=false;
 		}
    	drawButtons(BGraphic);
    	paint(getGraphics());
   	}while(true);

 

Umm.... if you set solvePressed to true inside solve()... then it will simply get set to false after it exits solve() and runs solvePressed=false; ... so how exactly are you expecting it to be set to true after that? What exactly are you trying to do? You are waiting for it to be set by the Solve button being pressed... then why not called solve() inside that actionlistener rather than going through the whole thing in a while loop?? All the GUI stuff should be in a different a thread anyways (automatically, you shouldn't have to do anything). If you are worried about having to reset all the buttons... then do that in solve or have a button / option for setting the board or something?

 

Am I missing something?

Posted

i dont call solve inside the action listener because i wanted it to be in an infinite loop before. after i knew it could actually solve it i was planning on fixing it to not be an infinite loop, and stop solving when its all filled in.

 

the if(solvePressed==true)

 

loop was only added as a troubleshooting measure.

 

the action listener sets solvePressed to true each time i click solve, i know its working because i tell it to print out solvePressed each time, and it keeps telling me True when i press the button.

 

i put another println at the very begining of solve, and i only ever see it once.

Posted

To create a button in awt you should be able to do Button a = new Button("NAME");

then add it to a panel or frame. Don't do a mouselistener do actionlistener.

in the constructor also:

a.addActionListener(this);

 

public void actionPerformed(ActionEvent e)

{

whatever you want done when the button is pressed.

}

 

If your using SWING all you would do is declar JButton a = new JButton();

I'll get you some files from my school tomorrow that thoroughly demonstrate swing.

Posted

i have a little info to add on the situation. im not sure what any of it means, or if its helpful, but...

 

if i enter one number, then tell it to solve, it does it. if i enter another number and press solve, it will do it as long as its not the same number as the first one, but it wont do a third.

 

if i enter two numbers and tell it to solve it will do it, and then stop.

 

if i enter one number, solve, and then enter the same number in a different spot, it stops.

 

 

by "it will do it" i mean it prints out all the right messages, draws new numbers to the screen, and it keeps going through the do while loop.

 

by "it stops" i mean it completely drops out of the do while loop, although it still responds to mouse clicks, and the solve button. it just never actually solves, and it doesnt draw any new numbers to the screen.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.