Hello coders
I am doing a matador game as a school project. Im am getting a pesky ArrayIndexOutOfBoundsException.
PlayerDB class
GameController class
Sry accidently posted. Here is the rest of the relevant code from GameController
The line that causes the exception is line 12 in 3rd code snippet.
Looking forward to some feedback.
I am doing a matador game as a school project. Im am getting a pesky ArrayIndexOutOfBoundsException.
PlayerDB class
package fields; import java.util.ArrayList; import java.util.List; import matador_BusinessLogic.Player; public class PlayerDB { public int numberOfPlayers; public Player[] allPlayers = new Player[numberOfPlayers]; public List<Player> playersInGame = new ArrayList<Player>(); public PlayerDB(int numberOfPlayers){ this.numberOfPlayers = numberOfPlayers; } public void createAndAddPlayers(String[] name, int[] carColor){ for (int i=0; i<numberOfPlayers; i++){ allPlayers[i] = new Player(name[i], carColor[i], i); playersInGame.add(allPlayers[i]); } } public Player nextPlayerInGame(Player currentPlayer) { int index = playersInGame.indexOf(currentPlayer); if (++index == playersInGame.size()) index = 0; return playersInGame.get(index); } }
GameController class
package matador_BusinessLogic; import fields.*; public class GameController { public static String[] allColors = new String[] { "Blå", "Grøn", "Rød","Gul", "Hvid", "Sort" }; public static MatadorRaflebaeger diceCup = new MatadorRaflebaeger(2, 6); public static MatadorFieldList board = new MatadorFieldList(); public static GameBoundary gb = new GameBoundary(); private PlayerDB db; private static Field currentField; private static Player currentPlayer; public void startGame() { final int STOP = 30000; // Spørger efter antallet af spillere og gemmer værdien i PlayerDB int numberOfPlayers = gb.askForPlayerCount(); // creates PlayerDB object and sets numberOfPlayers db = new PlayerDB(numberOfPlayers);
Sry accidently posted. Here is the rest of the relevant code from GameController
tring[] names = new String[db.numberOfPlayers]; int[] alreadyPickedColors = new int[db.numberOfPlayers]; for (int i = 0; i < db.numberOfPlayers; i++) { names[i] = gb.getName(i + 1); alreadyPickedColors[i] = gb.askForColor(allColors,alreadyPickedColors, i); } db.createAndAddPlayers(names, alreadyPickedColors); for (int i = 0; i < db.numberOfPlayers; i++) { gb.GuiAddPlayer(db.allPlayers[i].getName(), db.allPlayers[i].getMoney(), db.allPlayers[i].getCarColor()); } currentPlayer = db.playersInGame.get(0);
The line that causes the exception is line 12 in 3rd code snippet.
Looking forward to some feedback.