Jump to content

NullPointerError

- - - - -

  • Please log in to reply
6 replies to this topic

#1
jbdarcy

jbdarcy

    Newbie

  • Members
  • Pip
  • 3 posts
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;
}




}

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
you are double declaring the matrix.

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
jbdarcy

jbdarcy

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks....that's got it.....now on with the hard stuff.

#4
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
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
jbdarcy

jbdarcy

    Newbie

  • Members
  • Pip
  • 3 posts
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

#6
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
He means code tags in this forum, like this:

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
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Aye, you do it like this:

[noparse]
Java code goes here
[/noparse]




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users