Null Pointer Exception? What do I do?

My objects = null...but I can't think of anything else to do...

Page 1 of 1

7 Replies - 948 Views - Last Post: 03 December 2009 - 09:10 PM Rate Topic: -----

#1 Static Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Null Pointer Exception? What do I do?

Posted 03 December 2009 - 07:26 PM

I am working on a program that creates a tic-tac-toe game. The driver program creates objects based on who is playing the game (a human, an AI player that randomly selects spots, and an AI player that picks corners first), and I tried to get the program to only create the objects based on who is playing the game. Therefore, I created two objects and instantiated them with a "null" value, because the program would not compile without it.

I am trying to select a human vs. human game, but the Null Pointer Exception happens whenever a human is involved.

What do I do to fix this?


// GameDriver.java

import java.util.Scanner;

public class GameDriver {
 
  public static Board board;

  public static void main(String[] args) {
  
	Board board = new Board();
	Scanner scan = new Scanner(System.in);
	int playerX, playerO;
	GamePlayer player1 = null; //****** THIS IS MY PROBLEM!!! HOW CAN I GET AROUND THIS?
	GamePlayer player2 = null; // ***********************************************************
  
	System.out.println("Select a number for who will be Player \"X\": \n1. Human Player \n2. Random Player \n3. Corner Player ");
	playerX = scan.nextInt();
   
	switch(playerX) {
	  case 1:
		GamePlayer x = new GamePlayer('X');
		player1 = x;
		break;
	  case 2:
		RandomPlayer r = new RandomPlayer('X');
		player1 = r;
		break;
	  case 3:
		CornerPlayer c = new CornerPlayer('X');
		player1 = c;
		break;
	  default:
		System.out.println("Error, please choose a value between 1 and 3:");
	}

	System.out.println("Select a number for who will be Player \"O\": \n1. Human Player \n2. Random Player \n3. Corner Player ");
	playerO = scan.nextInt();
  
	  switch(playerO) {
	  case 1:
		GamePlayer x = new GamePlayer('X');
		player2 = x;
		break;
	  case 2:
		RandomPlayer r = new RandomPlayer('X');
		player2 = r;
		break;
	  case 3:
		CornerPlayer c = new CornerPlayer('X');
		player2 = c;
		break;
	  default:
		System.out.println("Error, please choose a value between 1 and 3:");
	}
	  while(board.isFull() == false) {
	  player1.play();
	  board.toString();
	  if(board.checkWinner() == 'X')
		System.out.println("Congratulations Player \"X\", you've won! Good game.");
	 
	  player2.play();
	  board.toString();
	  if(board.checkWinner() == 'O')
		System.out.println("Congratulations Player \"O\", you've won! Good game.");
	  if(board.isFull() == true && board.checkWinner() != 'X' && board.checkWinner() != 'O')
		System.out.println("Draw. Please play another game.");
  }
  }
}




I don't know if this is necessary, but this is the program that controls the play interactions for the human player.


// GamePlayer.java

import java.util.Scanner;

public class GamePlayer{

  protected char playerChar;
  Scanner scan = new Scanner(System.in);
 
  public GamePlayer (char playerChar) {
  
   playerChar = this.playerChar;
  }
 
  public void play () {
  
	System.out.println("Please enter the desired X-coordinate (0, 1, or 2) for the position.");
	int row = scan.nextInt();
	System.out.println("Please enter the desired Y-coordinate (0, 1, or 2) for the position.");
	int col = scan.nextInt();
  
	if(GameDriver.board.isOccupied(row, col) == false){
	  GameDriver.board.setPosition(row, col, playerChar);
	}
	  else
	  System.out.println("Sorry, this spot is taken. Please select new coordinates. ");




Any help would be greatly appreciated!

The program compiles, but the board will not print because of this error, which occurs after the user selects the coordinates.

Is This A Good Question/Topic? 0
  • +

Replies To: Null Pointer Exception? What do I do?

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Null Pointer Exception? What do I do?

Posted 03 December 2009 - 07:28 PM

Have the player object = new GamePlayer() rather then creating a new one and assigning it.
Was This Post Helpful? 0
  • +
  • -

#3 Static Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Re: Null Pointer Exception? What do I do?

Posted 03 December 2009 - 07:32 PM

View PostKYA, on 3 Dec, 2009 - 06:28 PM, said:

Have the player object = new GamePlayer() rather then creating a new one and assigning it.


Should I do this at the very beginning of the program or in the switch statement?
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Null Pointer Exception? What do I do?

Posted 03 December 2009 - 07:33 PM

in the switch, unless you want to institute a setCharacter() method in your class to set its char once the choices are made
Was This Post Helpful? 1
  • +
  • -

#5 Static Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Re: Null Pointer Exception? What do I do?

Posted 03 December 2009 - 07:36 PM

View PostKYA, on 3 Dec, 2009 - 06:33 PM, said:

in the switch, unless you want to institute a setCharacter() method in your class to set its char once the choices are made


Thank you so much for your quick and helpful response!
Was This Post Helpful? 0
  • +
  • -

#6 Static Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Re: Null Pointer Exception? What do I do?

Posted 03 December 2009 - 07:42 PM

Okay...so I tried that, and it did not get rid of the error. I still get a null pointer error. Is there anything else I can do besides setting the value of my objects to null?

 
switch(playerX) {
	  case 1:
		player1 = new GamePlayer('X');
		break;
	  case 2:
		player1 = new RandomPlayer('X');
		break;
	  case 3:
		player1 = new CornerPlayer('X');
		break;
	  default:
		System.out.println("Error, please choose a value between 1 and 3:");
	}

	System.out.println("Select a number for who will be Player \"O\": \n1. Human Player \n2. Random Player \n3. Corner Player ");
	playerO = scan.nextInt();
  
	  switch(playerO) {
	  case 1:
		player2 = new GamePlayer('X');
		break;
	  case 2:
		player2 = new RandomPlayer('X');
		break;
	  case 3:
		player2 = new CornerPlayer('X');
		break;
	  default:


Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Null Pointer Exception? What do I do?

Posted 03 December 2009 - 09:05 PM

updated code listing? it shouldn't [hypothetically] be throwing that if initialized and used correctly.
Was This Post Helpful? 0
  • +
  • -

#8 Riveted  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 87
  • Joined: 03-December 09

Re: Null Pointer Exception? What do I do?

Posted 03 December 2009 - 09:10 PM

Yeah, I didn't see any other place in this code that would cause that exact error, so I would guess that it's somewhere in your Board class.

This post has been edited by Riveted: 03 December 2009 - 09:19 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1