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.
This is called from a subclass Player in the function
REST OF CODE
Game class.
The Player 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;
}
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;
}
}
}
}
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();
}
}
