need help : Exception in thread "main" java.lang.NullPointerException

hey, im doing this project and am stuck with this...i have no idea why im getting this error!

Exception in thread "main" java.lang.NullPointerException

at boggleStage1.BoggleBoard.wordPath(BoggleBoard.java:109)

at boggleStage1.BoggleBoard.containsWord(BoggleBoard.java:74)

at boggleStage1.Player.checkOnBoard(Player.java:126)

at boggleStage1.Player.<init>(Player.java:68)

at boggleStage1.BoggleBoard.initBoard(BoggleBoard.java:24)

at boggleStage1.App.<init>(App.java:14)

at boggleStage1.App.main(App.java:39)

here are the classes / code :

private boolean checkOnBoard(String _input) {

boolean checkBoard = false;

System.out.println(word);

if(b1.containsWord(word))

checkBoard = true;

return checkBoard;

}

/**

* Tests whether a given word is found on the Boggle board. 8 way adjacency

* is used and no position in the board can be used twice in the same word.

*

* @return True if the word is found, false otherwise

*

* @param word

*The word to be tested.

*/

public boolean containsWord(String word) {

boolean validWord = false;

char firstLetter = word.charAt(0);

for (int i = 0; i < BOARD_SIZE; i++) {

for (int j = 0; j < BOARD_SIZE; j++) {

boolean[][] usedDice = new boolean[BOARD_SIZE][BOARD_SIZE];

74validWord = validWord || wordPath(i, j, word, usedDice);

}

}

return validWord;

}

/**

* Helper method for containsWord. Will test if the first character of a

* String is found at a given location and ensure that it has not been used

* already in this particular path. If it is found and has not been used,

* will then continue to look for the remainder of the word using 8 way

* adjacency.

*

* @return True if the word length gets to zero, false if the letter

* is not found or the position has been used.

*

* @param row

*The row to be tested

* @param col

*The column to be tested

* @param word

*The word that is path is being found for

* @param usedDice

*An array that retains positions of the board that have already

*been used.

*/

private boolean wordPath(int row, int col, String word, boolean[][] usedDice) {

if (word.length() == 0) {

return true;

}

char testLetter = word.charAt(0);

109if (_board[row][col] != testLetter || usedDice[row][col] == true) {

return false;

}

usedDice[row][col] = true;

boolean validPath = false;

boolean rowUp = false;

boolean rowDown = false;

boolean colLeft = false;

boolean colRight = false;

if (row - 1 >= 0) {

rowUp = true;

}

if (row + 1 < BOARD_SIZE) {

rowDown = true;

}

if (col - 1 >= 0) {

colLeft = true;

}

if (col + 1 < BOARD_SIZE) {

colRight = true;

}

if (rowUp) {

validPath = validPath

|| wordPath(row - 1, col, word.substring(1, word.length()),

usedDice);

if (colLeft) {

validPath = validPath

|| wordPath(row - 1, col - 1, word.substring(1, word

.length()), usedDice);

}

if (colRight) {

validPath = validPath

|| wordPath(row - 1, col + 1, word.substring(1, word

.length()), usedDice);

}

}

if (rowDown) {

validPath = validPath

|| wordPath(row + 1, col, word.substring(1, word.length()),

usedDice);

if (colLeft) {

validPath = validPath

|| wordPath(row + 1, col - 1, word.substring(1, word

.length()), usedDice);

}

if (colRight) {

validPath = validPath

|| wordPath(row + 1, col + 1, word.substring(1, word

.length()), usedDice);

}

}

if (colLeft) {

validPath = validPath

|| wordPath(row, col - 1, word.substring(1, word.length()),

usedDice);

}

if (colRight) {

validPath = validPath

|| wordPath(row, col + 1, word.substring(1, word.length()),

usedDice);

}

return validPath;

}

Please help, i have no idea why its not working!

Thanks in advance

[4344 byte] By [ilxlsta] at [2007-11-27 9:22:08]
# 1
109 if (_board[row][col] != testLetter || usedDice[row][col] == true) {If that line is causing the NPE, then at least one of the following is null:boardboard[row]usedDiceusedDice[row]
jverda at 2007-7-12 22:16:25 > top of Java-index,Java Essentials,Java Programming...
# 2
wow! that was a quick reply!how should i fix this exactly?!thanks
ilxlsta at 2007-7-12 22:16:25 > top of Java-index,Java Essentials,Java Programming...
# 3

> wow! that was a quick reply!

>

> how should i fix this exactly?!

>

> thanks

Which IDE are you using? If you are using one with a Debugger then switch to debug mode and step through your code.

EDIT:

Are you dealing with Objects or primitives in your Array? I can't make out from your code, it makes my eyes sting.

Try to use code tags please next time

http://forum.java.sun.com/help.jspa?sec=formatting

:)

Message was edited by:

_helloWorld_

_helloWorld_a at 2007-7-12 22:16:25 > top of Java-index,Java Essentials,Java Programming...
# 4

> wow! that was a quick reply!

>

> how should i fix this exactly?!

1) Figure out which ones are null using print statements or a debugger.

2) Initialize them so that they are not null.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

jverda at 2007-7-12 22:16:25 > top of Java-index,Java Essentials,Java Programming...
# 5

sorry!! new to java, never put up codes on msg boards, so didnt know practices

private boolean checkOnBoard(String _input) {

boolean checkBoard = false;

System.out.println(word);

if(b1.containsWord(word))

checkBoard = true;

return checkBoard;

}

/**

* Tests whether a given word is found on the Boggle board. 8 way adjacency

* is used and no position in the board can be used twice in the same word.

*

* @return True if the word is found, false otherwise

*

* @param word

* The word to be tested.

*/

public boolean containsWord(String word) {

boolean validWord = false;

char firstLetter = word.charAt(0);

for (int i = 0; i < BOARD_SIZE; i++) {

for (int j = 0; j < BOARD_SIZE; j++) {

boolean[][] usedDice = new boolean[BOARD_SIZE][BOARD_SIZE];

74 validWord = validWord || wordPath(i, j, word, usedDice);

}

}

return validWord;

}

/**

* Helper method for containsWord. Will test if the first character of a

* String is found at a given location and ensure that it has not been used

* already in this particular path. If it is found and has not been used,

* will then continue to look for the remainder of the word using 8 way

* adjacency.

*

* @return True if the word length gets to zero, false if the letter

* is not found or the position has been used.

*

* @param row

* The row to be tested

* @param col

* The column to be tested

* @param word

* The word that is path is being found for

* @param usedDice

* An array that retains positions of the board that have already

* been used.

*/

private boolean wordPath(int row, int col, String word, boolean[][] usedDice) {

if (word.length() == 0) {

return true;

}

char testLetter = word.charAt(0);

109 if (_board[row][col] != testLetter || usedDice[row][col] == true) {

return false;

}

usedDice[row][col] = true;

boolean validPath = false;

boolean rowUp = false;

boolean rowDown = false;

boolean colLeft = false;

boolean colRight = false;

if (row - 1 >= 0) {

rowUp = true;

}

if (row + 1 < BOARD_SIZE) {

rowDown = true;

}

if (col - 1 >= 0) {

colLeft = true;

}

if (col + 1 < BOARD_SIZE) {

colRight = true;

}

if (rowUp) {

validPath = validPath

|| wordPath(row - 1, col, word.substring(1, word.length()),

usedDice);

if (colLeft) {

validPath = validPath

|| wordPath(row - 1, col - 1, word.substring(1, word

.length()), usedDice);

}

if (colRight) {

validPath = validPath

|| wordPath(row - 1, col + 1, word.substring(1, word

.length()), usedDice);

}

}

if (rowDown) {

validPath = validPath

|| wordPath(row + 1, col, word.substring(1, word.length()),

usedDice);

if (colLeft) {

validPath = validPath

|| wordPath(row + 1, col - 1, word.substring(1, word

.length()), usedDice);

}

if (colRight) {

validPath = validPath

|| wordPath(row + 1, col + 1, word.substring(1, word

.length()), usedDice);

}

}

if (colLeft) {

validPath = validPath

|| wordPath(row, col - 1, word.substring(1, word.length()),

usedDice);

}

if (colRight) {

validPath = validPath

|| wordPath(row, col + 1, word.substring(1, word.length()),

usedDice);

}

return validPath;

}

ilxlsta at 2007-7-12 22:16:25 > top of Java-index,Java Essentials,Java Programming...
# 6
> sorry!! new to java, never put up codes on msg> boards, so didnt know practicesUnfortunately, you copy/pasted from your original post here, which had already lost its indenting. Next time, copy/paste from your properly indented source.
jverda at 2007-7-12 22:16:25 > top of Java-index,Java Essentials,Java Programming...
# 7

As jverd mentioned the problem is with your arrays. You have probably done something like this:

//This creates an array of Objects and all the values are null.

MyObject [] obj = new Object[10]

//You need to give them the values your program expects

//This is one way

obj[0] = new Object;

Message was edited by:

_helloWorld_

_helloWorld_a at 2007-7-12 22:16:25 > top of Java-index,Java Essentials,Java Programming...
# 8
i think the problem lies at :boolean[][] usedDice = new boolean[BOARD_SIZE][BOARD_SIZE];if i try to print out that array, i get only 4 values instead of 16.
ilxlsta at 2007-7-12 22:16:25 > top of Java-index,Java Essentials,Java Programming...
# 9
And...?
jverda at 2007-7-12 22:16:25 > top of Java-index,Java Essentials,Java Programming...
# 10
dont know!!!! if i comment out :validWord = validWord || wordPath(i, j, word, usedDice);problem is removed. but unfortunately, i need that!i have no idea how to fix this.
ilxlsta at 2007-7-12 22:16:25 > top of Java-index,Java Essentials,Java Programming...