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("Which piece would you like to move? "
piece = UserInput.readInt();
System.out.print(" Where would you like to move the piece? "
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;
}
}
"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("Which piece would you like to move? "
piece = UserInput.readInt();
System.out.print(" Where would you like to move the piece? "
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;
}
}