Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 5th, 2011, 08:36 PM
Registered User
 
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to make nested array of class

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

Last edited by NickJG; November 5th, 2011 at 08:52 PM.. Reason: trying to format it to read better on this posting window
 
Old November 14th, 2011, 07:10 PM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Quote:
Originally Posted by NickJG View Post
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
Well, you need to put stuff in your array, otherwise you will be pointing at something null!

You can do this (explained inline):

Code:
charlie@localhost:~$ cat TestCode.java 
class City {    
    String  cityName;                                   
    int[][] buildingList = new int[10][2]; // creates array, but doesn't populate it
    public City(String name) {  // Now we have a constructor
        this.cityName=name;
                // Fill up the array "slots" with data
        for(int i=0;i<10;i++) {
            for(int j=0;j<2;j++) {
                this.buildingList[i][j]=i*j;
            }
        }
    }    
}
class Player {    
             String playerName;                                    
             City[] cityList = new City[10]; // creates but doesn't populate
}
public class TestCode {
              public static void main(String[] args) {
              Player player1 = new Player();                  
          player1.cityList[1]=new City("London"); // Have to "instantiate" your object
              player1.cityList[1].buildingList[1][1] = 5;
          System.out.println("Expect 5: " + player1.cityList[1].buildingList[1][1]); // Write a test ;-)
    }    
}
charlie@localhost:~$ javac TestCode.java 
charlie@localhost:~$ java TestCode 
Expect 5: 5
__________________
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
 
Old February 23rd, 2012, 10:50 AM
Registered User
 
Join Date: Feb 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

Quote:
Originally Posted by NickJG View Post
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
That is because you do not init player1.cityList[1],player1.cityList[1] is null





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make a nested list tag that is valid? chobo2 CSS Cascading Style Sheets 1 April 21st, 2009 04:07 AM
How to make function which returns array? sektor Excel VBA 1 April 11th, 2009 01:05 PM
how do i make a boolean array. helppp Un33k Visual Basic 2005 Basics 2 June 14th, 2007 06:56 AM
appending to nested array asinning Beginning PHP 1 March 2nd, 2005 10:38 AM
How to make a class public johnjonsson VS.NET 2002/2003 0 October 6th, 2003 02:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.