Hi all,
I'm working on an extra credit assignment. The class I created for it has three methods that are supposed to share the same array. But when they go to access the array the program throws a null pointer error. Not sure what I did wrong.....
Jeff
import java.util.*;
public class Board
{
/**
* Attributes
*/
int size;
int move;
int[][] matrix;
/**
* Constructor
*/
public Board (int n)
{
size = n;
move = 1;
int[][]matrix = new int [size+4][size+4];
matrix [1][1] = 8;
System.out.println (matrix[1][1]);
}
/**
* Initialize the Board with -1's and 0's
*/
public int[][] initialize(int n)
{
int p = n+4;
int grid [][] = new int [p][p];
for (int i = 0;i < (p); i++)
{
for (int j = 0; j < (p); j++)
{
grid [i][j] = -1;
}
}
for (int i = 2;i < (p-2); i++)
{
for (int j = 2; j < (p-2); j++)
{
grid [i][j] = 0;
}
}
/**
* Print Initial Board Before First Move
*/
if (this.move == 1)
for (int k = 0; k < (p); k++)
{
for (int l = 0; l < (p); l ++)
{
System.out.print(grid[l][k]);
System.out.print (" ");
if (((grid[l][k] == 0)&& (l > 1 && l < (p-3)))|| (grid[l][k] == -1 && l == 1 && (k > 1 && k < (p-2))))
System.out.print(" ");
}
System.out.println();
}
System.out.println();
System.out.println("Your board is ready!");
return grid;
}
public boolean tour (int row, int col)
{
/*if (move == 1)
{
int z = this.size;
initialize (z);
}
*/
//System.out.println (bd [3][2]);
boolean done = false;
if (valid (row, col))
{
matrix[row][col] = move;
if (move == (size * size))
done = true;
else
{
done = tour (row-2, col-1);
if (!done)
done = tour (row-2, col+1);
if (!done)
done = tour (row-1, col+2);
if (!done)
done = tour (row-1, col-2);
if (!done)
done = tour (row+1, col+2);
if (!done)
done = tour (row+1, col-2);
if (!done)
done = tour (row+2, col+1);
if (!done)
done = tour (row+2, col-1);
}
if (done)
matrix[row][col]=move;
move++;
}
return done;
}
private boolean valid (int row, int col)
{
boolean result = false;
if (matrix[row][col] != -1)
if (matrix[row][col] == 0)
result = true;
return result;
}
}
6 replies to this topic
#1
Posted 10 December 2011 - 09:14 PM
|
|
|
#2
Posted 11 December 2011 - 03:33 AM
you are double declaring the matrix.
The declaration in the contructor
you need to lose the int[][] because otherwise you are making a new variable, which is independent from the field int[][] matrix in the Board class.
The declaration in the contructor
int[][]matrix = new int [size+4][size+4];Overrides the field matrix.
you need to lose the int[][] because otherwise you are making a new variable, which is independent from the field int[][] matrix in the Board class.
matrix = new int [size+4][size+4];
#3
Posted 11 December 2011 - 10:08 AM
Thanks....that's got it.....now on with the hard stuff.
#4
Posted 11 December 2011 - 02:49 PM
jbdarcy use the code tags; it makes your code a lot easier to read. Next time select your code then click #
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#5
Posted 11 December 2011 - 03:48 PM
Fread,
Good advice. Thanks. Did some tagging but got so involved in solving that I stopped. Adding more now. I can attached the updated code if you are interested in looking at it. Still working on the logic.
Jeff
Good advice. Thanks. Did some tagging but got so involved in solving that I stopped. Adding more now. I can attached the updated code if you are interested in looking at it. Still working on the logic.
Jeff
#6
Posted 12 December 2011 - 12:51 AM
He means code tags in this forum, like this:
You shouldn't comment your code unless it is necessary. It actually makes the code less readable.
public boolean tour (int row, int col){
if (move == 1){
int z = this.size;
initialize (z);
}
}
You shouldn't comment your code unless it is necessary. It actually makes the code less readable.
#7
Posted 12 December 2011 - 01:16 AM
Aye, you do it like this:
[noparse]
[noparse]
Java code goes here[/noparse]
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









