Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java.lang.NullPointerException 1

Status
Not open for further replies.

jimache

Programmer
Feb 26, 2003
15
US
Hi. I'm new with java and i get this error message:
"Exception in thread "main" java.lang.NullPointerException
at P4.init(P4.java:48)
at P4.main(P4.java:12)"

The program compiles fine but when I run the program i get the error message. What am I doing wrong? Below is my code.( the class GameBoard is ok, updateBoard, printBoard, and validMove are methods of GameBoard class). Can somebody help me figure it out. Thank you.


import java.io.*;
class P4
{
public static GameBoard pegBoard;
public static int move;
public static int piece;

public static void main(String args[])

{
GameBoard pegBoard= new GameBoard(15);
init();
System.out.println(" xxxxxxxxxxx");
System.out.println(" xxxxxxxxxxx\n");

System.out.println(" ______");
System.out.println(" / 0 \\");
System.out.println(" / 1 2 \\");
System.out.println(" / 3 4 5 \\");
System.out.println(" / 6 7 8 9 \\");
System.out.println(" /10 11 12 13 14\\");
System.out.println(" ----------------\n");

pegBoard.printBoard();
while(endOfGame() )
{
getInput();
if (pegBoard.validMove(move, piece))
{
pegBoard.updateBoard(move, piece);
pegBoard.printBoard();
}
else
{
System.out.println(" Invalid Move");
getInput();
}
}
System.out.println("GoodBye");

}

public static void init()
{
int i;
for(i=0; i< pegBoard.board.length; i++)
{
pegBoard.board='*';
}
pegBoard.board[12]=' ';
}

public static void getInput()

{
System.out.println(&quot;Which piece would you like to move? &quot;);
piece = UserInput.readInt();
System.out.print(&quot; Where would you like to move the piece? &quot;);
move = UserInput.readInt();
}

public static boolean endOfGame()
{
int i,j;
boolean r = true;
pegBoard.updateBoard(piece, move);
for(i=0; i< pegBoard.board.length; i++)
for(j=0; j< pegBoard.board.length; j++)
{
if(pegBoard.validMove(i,j))
{
r = true;
break;
}
else
r =false;
}
return r;
}
}
 
i don't know anything about your GameBoard class, so it's hard to say, but it looks like it's failing when trying to change the &quot;board&quot; field of pegboard. If I were you, find out the value of pegboard, pegboard.board, and pegboard.length as soon as you get inside the &quot;init()&quot; method (either through breakpoints or printlns).

any reason to access the field directly as opposed to having get and set methods? Liam Morley
lmorley@gdc.wpi.edu
&quot;light the deep, and bring silence to the world.
light the world, and bring depth to the silence.&quot;
 
In the main method you have the line..

GameBoard pegBoard= new GameBoard(15);

pegBoard is therefore in the scope of the main method, not the class. So change the line to ..

pegBoard= new GameBoard(15);

This should then set the class variable pegBoard to the new instance of GameBoard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top