I'm trying to create a data structure where there are several players. Each player has several cities. Each city has several buildings. Sounds easy however I need the cities form different players to be able to share the same names and the buildings from different cities to also share names.
I have come up with the following code that would be perfect for my needs and it compiles ok however when I run it I get "Exception in thread "main" java.lang.NullPointerException" error. I really would somehow like to make a array of a class as shown in the following code. I also spent many hours trying to achieve the same thing with nested classes but also with no luck.
Code:
***********************************************
public class City {
String cityName;
int[][] buildingList = new int[10][2];
}
***********************************************
public class Player {
String playerName;
City[] cityList = new City[10]}
***********************************************
public class TestCode {
public static void main(String[] args) {
Player player1 = new Player();
player1.cityList[1].buildingList[1][1] = 5;
/* (player1.cityList[1].buildingList[1][1] = 5;) gives me the following error after trying run.
Exception in thread "main" java.lang.NullPointerException at testCode.main(TestCode.java:9) */
}
}
***********************************************
Actually I would like to make the player list a array also as in player[10] but for now player1, player2 will also work.
Please help. Thanks
Nick