java inheritance help

Borkil

Senior member
Sep 7, 2006
248
0
0
Hi I'm making a simple game in Java but I'm a little confused on why I'm getting a null exception in this function on "drawPile". I think I'm accessing a different drawPile in the child class.

Code:
	public GamePiece drawPiece(){
		Random rand = new Random();
		[b]System.out.println(this.drawPile.size()); //EXCEPTION!![/b]
		int randInt = rand.nextInt(this.drawPile.size());
		System.out.println("RANDOM INT: " + randInt);
		GamePiece drawnPiece = this.drawPile.get(randInt);
		this.drawPile.remove(randInt);
		return drawnPiece;
	}
This is called from a subclass Player in the function
Code:
	public void playerTurn(){
		currentPiece = this.drawPiece();
	}

REST OF CODE
Code:
public class Main {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Game game = new Game();
		game.newGame();

	}
}

Game class.

Code:
import java.util.*;


public class Game {
	private GameBoard gameBoard; //the gameboard
	private HashMap<Integer, Player> players; //holds the player class
	private HashMap<Character, Integer> pieces; //holds the piece types and how many of each.
	private ScoreBoard scoreBoard; //the scoreboard
	private ArrayList<GamePiece> drawPile; //the pile of pieces that players draw from. game ends when empty
	private int playerCount; //# of players
	
	public void newGame(){

		this.gameBoard = new GameBoard();
		
		this.generatePlayers();
		this.generateDrawPile();
		this.drawPile.remove(drawPile.size() - 1);
		this.printDrawPile();
		System.out.println("***********NEW GAME**************");
		this.playGame();
	}
	
	public Game(){
	}
	
	public GamePiece drawPiece(){
		Random rand = new Random();
		System.out.println(this.drawPile.size());
		int randInt = rand.nextInt(this.drawPile.size());
		System.out.println("RANDOM INT: " + randInt);
		GamePiece drawnPiece = this.drawPile.get(randInt);
		this.drawPile.remove(randInt);
		return drawnPiece;
	}
	
	public static int getINTInput(String inst){
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		while(!input.hasNextInt()){
			System.out.println("Invalid input");
			System.out.println(inst);
			input.next();
		}
		return input.nextInt();
	}
	
	private void generateDrawPile(){
		this.drawPile = new ArrayList<GamePiece>();
		this.pieces = new HashMap<Character, Integer>();
		

		this.pieces.put('A', 2);
		this.pieces.put('B', 4);

		for(char key : this.pieces.keySet()){
			//System.out.println(key + " " + pieces.get(key));
			for(int i = 0; i < pieces.get(key); i++){
				this.drawPile.add(new GamePiece(key));
			}
		}
	}
	
	private void generatePlayers(){
		this.players = new HashMap<Integer, Player>();
		
		do{
			System.out.println("Input the number of players (2-5)");
			this.playerCount = Game.getINTInput("Input the number of players (2-5)");
			if (this.playerCount > 5 || this.playerCount < 2){
				System.out.println("Invalid input");
			}
		}while(this.playerCount > 5 || this.playerCount < 2);
		
		for(int i = 1; i <= this.playerCount; i++){
			this.players.put(i, new Player("Player" + Integer.toString(i)));
			this.players.get(i).printPlayerInfo();
		}
		
		
	}
	
	
	private void playGame(){
		int currentPlayer = 1;
		while(!this.drawPile.isEmpty()){
			this.players.get(currentPlayer).playerTurn();
			currentPlayer++;
			if(currentPlayer > this.playerCount){
				currentPlayer = 1;
			}
		}
	}
}
The Player class
Code:
public class Player extends Game {
	private String playerName;
	private int score;
	private GamePiece currentPiece;

	
	public Player(String name){
		this.playerName = name;
		this.score = 0;

	}

	public void playerTurn(){
		currentPiece = this.drawPiece();
	}
	

}
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
OK, the Game has-a Player. That's fine. But the Player is-a Game? :confused:

I think you're accessing the drawPile in the same class, but in a different instance.

Edit: If you want a further hint about what's going wrong, click here. ;)
 
Last edited:

Borkil

Senior member
Sep 7, 2006
248
0
0
lol well I was originally thinking that a game has players. Plus I wanted the players to have access to the drawPile. what's another way for the players to access the instance of the drawPile?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
Perhaps each Player needs to know what (instance of a) Game they are playing?