Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 5th, 2011, 08:36 PM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #2 (permalink)  
Old November 14th, 2011, 06:10 PM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
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
Reply With Quote
  #3 (permalink)  
Old February 23rd, 2012, 09:50 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 09:38 AM
How to make a class public johnjonsson VS.NET 2002/2003 0 October 6th, 2003 02:17 PM



All times are GMT -4. The time now is 06:08 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.