Dostaję:
Exception in thread "main" java.lang.NullPointerException at Ships.Ship.getRandomNum(Ship.java:60) at Ships.Ship.setShip(Ship.java:45) at Ships.Player.setShipNames(Player.java:83) at Ships.Main.main(Main.java:46)
Jak zmienić funkcję getRandomNum? Ma ona za zadanie wylosować dowolne pole z z tablicy i sprawdzić czy już nie jest zajęte.
private int getRandomNum() {
Random rand = new Random();
do {
randomNum = rand.nextInt(100 - length - 1);
} // - length cause every ship takes x random fields -length fields before end of a floor
while (!takenFields.contains(randomNum));
return randomNum;
}
Kod źródłowy: https://github.com/phoeeenix/ShipsGame
P.S. Gdy ominę funkcję i usunę blok do-while z funkcji to dostaję następny:
Exception in thread "main" java.lang.NullPointerException at Ships.Player.setShipNames(Player.java:85) at Ships.Main.main(Main.java:46)
Nie wiem dlaczego on bierze wartość null w 85 wierszu przy dodawaniu obiektu do listy: shipsGroup.add(ship);