Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

realNim game and NullPointerException

I'm having a bit of trouble with this assignment. Most of the code was already written and we only had to implement 2 short functions. The program compiles but when you run it it gives a null pointer exception. Can anyone help me locate the problem with it? Is it something that our instructor coded incorrectly? I apologize for the length of the code. Thanks in advance for an help.

here's the error given:
NullPointerException:
at realNim.Computer.setMove(Computer.java:76)
at realNim.Computer.chooseMove(Computer.java:40)
at realNim.RealNimGame.play(RealNimGame.java:95)
at realNim.RealNimInterface.start(RealNimInterface.java:42)
at realNim.Top.main(Top.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

and the code to the sections that it's running into problems with:

package realNim;

/**
* The computer contestant in the game of RealNim.
*/
public class Computer{

//Instance Variables:
private int numToTake = 0; // number of sticks computer will take
private Move move; //the most recent move chosen by this contestant
private Board board = new Board();
//Constructors:
/**
* Create a computer contestant.
*/
public Computer(){
//STUDENTS: what actions, if any, are needed in this constructor?

}//end of constructor Computer()

//Queries:

/**
* The most recent move chosen by this contestant.
*/
public Move getMove(){
return this.move;
}//end of query getMove()

//Commands:

/**
* The computer contestant chooses the move it will make.
* The computer chooses to remove half the sticks on the
* highest numbered non-empty row.
*/
public void chooseMove( Board board ){
if (board.getRow(5).getNumSticks() > 0){
numToTake = ((board.getRow(5).getNumSticks() / 2)+(board.getRow(5).getNumSticks() % 2));
this.setMove(5, numToTake);
}
else if (board.getRow(4).getNumSticks() > 0){
numToTake = ((board.getRow(4).getNumSticks() / 2)+(board.getRow(4).getNumSticks() % 2));
this.setMove(5, numToTake);
}
else if (board.getRow(3).getNumSticks() > 0){
numToTake = ((board.getRow(3).getNumSticks() / 2)+(board.getRow(3).getNumSticks() % 2));
this.setMove(5, numToTake);
}
else if (board.getRow(2).getNumSticks() > 0){
numToTake = ((board.getRow(2).getNumSticks() / 2)+(board.getRow(2).getNumSticks() % 2));
this.setMove(5, numToTake);
}
else if (board.getRow(1).getNumSticks() > 0){
numToTake = ((board.getRow(1).getNumSticks() / 2)+(board.getRow(1).getNumSticks() % 2));
this.setMove(5, numToTake);
}

}//end of command chooseMove( Board )

/**
* Set the row and number of sticks for removal as chosen by
* this contestant.
* require:
* 1 <= row
* 1 <= sticksToRemove
*/
public void setMove( int row, int sticksToRemove ){
assert 1 <= row;
assert 1 <= sticksToRemove;
this.move.setMove(row, sticksToRemove);
}//end of command setMove( int, int )

/**
* Set the move of this contestant.
*/
public void setMove( Move move ){
this.move = move;
}//end of command setMove( Move )

}//end of class Computer

package realNim;

import java.util.Scanner;

/**
* The user interface for the game of RealNim.
*/
public class RealNimInterface{

//Instance Variables:

private RealNimGame theGame; //the model being observed
private Scanner input; //the input stream of characters
/*(the output stream of characters
will be the default: System.out) */
//Constructors:

/**
* Create a new user interface.
*/
public RealNimInterface(){
this.theGame = new RealNimGame( this ); //create the model and
//..register with it
input = new Scanner(System.in); //the default input stream
}//end of constructor RealNimInterface()

//Queries: none.

//Commands:

/**
* Start the interface's observation of a game of RealNim.
*/
public void start(){
System.out.println("Welcome to the game of RealNim.");
System.out.println("Here is the initial board configuration:");
System.out.println(); //make a blank line in output
displayBoard( theGame.getBoard());
System.out.println("You will take the first turn.");
System.out.println();

theGame.play();
}//end of command start()

/**
* Respond to an interesting event in the model.
* require:
* code == RealNimGame.HUMAN_NEEDS_MOVE ||
* code == RealNimGame.HUMAN_TURN_COMPLETED ||
* code == RealNimGame.COMPUTER_TURN_COMPLETED ||
* code == RealNimGame.GAME_OVER
*/
public void update( int code ){

if (code == RealNimGame.HUMAN_NEEDS_MOVE){
//get the human user's choice of a move
Move move = extractUserMove();
theGame.getHuman().setMove(move);
}
else if (code == RealNimGame.HUMAN_TURN_COMPLETED){
System.out.println("The human chose to remove " +
theGame.getHuman().getMove().getSticksToRemove() +
" sticks from row " +
theGame.getHuman().getMove().getRowNumber() +
", with result:");
System.out.println();
displayBoard( theGame.getBoard());
}
else if (code == RealNimGame.COMPUTER_TURN_COMPLETED){
System.out.println("The computer chose to remove " +
theGame.getComputer().getMove().getSticksToRemove() +
" sticks from row " +
theGame.getComputer().getMove().getRowNumber() +
", with result:");
System.out.println();
displayBoard( theGame.getBoard());
}
else if (code == RealNimGame.GAME_OVER){
System.out.println("The game is now over. The winner is the " +
theGame.getWinner() + ".");
System.out.println();
}
}//end of command update( int )


/*
Extract a valid move from the human user
*/
private Move extractUserMove(){
//the compiler sometimes balks if local variables are not initialized
int rowChoice = 1; //user input: row number
int sticksToRemove = 1; //user input: sticks to remove

boolean goodData = false; //remains false until user supplies
//..valid input data
while ( !goodData ){
System.out.println("On the same line, please enter a row number and " +
"number of sticks to remove:");
System.out.flush(); //make sure user sees entire prompt
rowChoice = input.nextInt();
if (rowChoice < 1 || rowChoice > theGame.getBoard().numRows()){
System.out.println("The row number must be in the range 1.." +
theGame.getBoard().numRows() + "; please try again.");
System.out.flush();
input.nextLine(); //gobble remainder of input line
}
else if (theGame.getBoard().getRow(rowChoice).getNumSticks() == 0){
System.out.println("Row " + rowChoice + " is empty; please try again.");
System.out.flush();
input.nextLine(); //gobble remainder of input line
}
else{ //now got valid row number; go on to sticks-to-remove
sticksToRemove = input.nextInt();
if (sticksToRemove < 1 || sticksToRemove >
theGame.getBoard().getRow(rowChoice).getNumSticks()){
System.out.println("The number of sticks you remove from row " +
rowChoice + " must be between 1 and " +
theGame.getBoard().getRow(rowChoice).getNumSticks()+
";\nplease try again.");
System.out.flush();
input.nextLine();
}
else //both input data values are valid
goodData = true;
}//end else{ //now got valid row..
}//end while ( !goodData )
return new Move(rowChoice, sticksToRemove);
}//end command extractUserMove()

/*
Display the board.
*/
private void displayBoard( Board board ){
System.out.println("Row 1: " + board.getRow(1).getNumSticks());
System.out.println("Row 2: " + board.getRow(2).getNumSticks());
System.out.println("Row 3: " + board.getRow(3).getNumSticks());
System.out.println("Row 4: " + board.getRow(4).getNumSticks());
System.out.println("Row 5: " + board.getRow(5).getNumSticks());
System.out.println();
System.out.flush();
}//end of command displayBoard

}//end of class RealNimInterface

package realNim;

/**
* Top-level class to hold the command "main".
*/

public class Top{

public static void main( String[] args ){

RealNimInterface ui = new RealNimInterface();
ui.start();

}//end of command main

}//end of class Top
[10242 byte] By [ndnbrlgrnd] at [2007-11-11 10:20:39]
# 1 Re: realNim game and NullPointerException
this.move.setMove(row, sticksToRemove);

Insert the line System.out.println(this.move); above the mentioned line.
What does it print?
aniseed at 2007-11-11 22:31:41 >
# 2 Re: realNim game and NullPointerException
Here's what i ended up with after working on it for a while:
(had to change chooseMove() and add move into the constructor)

package realNim;

/**
* The computer contestant in the game of RealNim.
*/
public class Computer{

//Instance Variables:
private int numToTake = 0; // number of sticks computer will take
private int isPositive = 0; // whether or not a the number of sticks is greater than 0
private int numOfRow = 5; // number of the row the computer will take from
private Move move; //the most recent move chosen by this contestant
private Board board = new Board();
//Constructors:
/**
* Create a computer contestant.
*/
public Computer(){
//STUDENTS: what actions, if any, are needed in this constructor?
this.move = new Move();
}//end of constructor Computer()

//Queries:

/**
* The most recent move chosen by this contestant.
*/
public Move getMove(){
return this.move;
}//end of query getMove()

//Commands:

/**
* The computer contestant chooses the move it will make.
* The computer chooses to remove half the sticks on the
* highest numbered non-empty row.
*/
public void chooseMove( Board board ){
isPositive = 0;
numOfRow = 5;
while (isPositive == 0){
if (board.getRow(numOfRow).getNumSticks() > 0){
numToTake = ((board.getRow(numOfRow).getNumSticks() / 2)+(board.getRow(numOfRow).getNumSticks() % 2));
this.setMove(numOfRow, numToTake);
isPositive ++;
}
else
numOfRow --;
}
}//end of command chooseMove( Board )

/**
* Set the row and number of sticks for removal as chosen by
* this contestant.
* require:
* 1 <= row
* 1 <= sticksToRemove
*/
public void setMove( int row, int sticksToRemove ){
assert 1 <= row;
assert 1 <= sticksToRemove;
System.out.println(row);
System.out.println(sticksToRemove);
this.move.setMove(row, sticksToRemove);
}//end of command setMove( int, int )

/**
* Set the move of this contestant.
*/
public void setMove( Move move ){
this.move = move;
}//end of command setMove( Move )

}//end of class Computer
ndnbrlgrnd at 2007-11-11 22:32:35 >
# 3 Re: realNim game and NullPointerException
Attach software www.cajoon.com to your Java environment (J2SE, J2EE) and get 'root-cause' for every exception automatically . . .
abarnea_2000 at 2007-11-11 22:33:34 >