Results 1 to 7 of 7
  1. #1
    emerald000 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Problem with an array of objects

    First, the code to its bare-bones:

    Java Code:
    public class FooMain {
    
    	public static void main(String[] args) {
    		
    		Foo[] bar = new Foo[3];
    		
    		System.out.println(bar[0].x); //Error thrown here
    	
    	}
    }
    
    public class Foo {
    
    	public int x;
    	
    	public Foo() {
    	
    		x = 12;
    		
        }
    	
    }
    What I want to do is to create an array where each element of the array is a Foo object. By calling bar[0], I get the reference to the first Foo object, .... The code above compiles without a problem, but I get a null pointer exception on the line indicated above.

    I really don't know what the problem is. If I create a single Foo (not in an array), it works correctly. If I create an array of int, it works too. But an array of Foo just won't work.

    Thanks in advance for your help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with an array of objects

    Object arrays are collections of variables, not objects, and they have to be filled with objects to work. One way to think of arrays is as if you were creating a parking lot. Starting out it has no cars, and you have to fill it with cars for each space to have any "meaning".

  3. #3
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: Problem with an array of objects

    You have to initialize objects shown below
    Quote Originally Posted by emerald000 View Post
    First, the code to its bare-bones:

    Java Code:
    public class FooMain {
    
    	public static void main(String[] args) {
    		
    		Foo[] bar = new Foo[3];
    
    		////////////////////
    
                    for(Foo f : bar){
                         f = new Foo();
                    }
                    /////////////////////
    		System.out.println(bar[0].x); //Error thrown here
    	
    	}
    }
    
    public class Foo {
    
    	public int x;
    	
    	public Foo() {
    	
    		x = 12;
    		
        }
    	
    }
    What I want to do is to create an array where each element of the array is a Foo object. By calling bar[0], I get the reference to the first Foo object, .... The code above compiles without a problem, but I get a null pointer exception on the line indicated above.

    I really don't know what the problem is. If I create a single Foo (not in an array), it works correctly. If I create an array of int, it works too. But an array of Foo just won't work.

    Thanks in advance for your help.
    Last edited by yellowledbet; 02-27-2012 at 04:39 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with an array of objects

    Quote Originally Posted by yellowledbet View Post
    You have to initialize objects shown below
    That won't work as you can't initialize items in an array using a for-each loop. Test it yourself and you'll see. You need to use a for loop.

    e.g.

    Java Code:
          // for (Foo f : bar) {
          // f = new Foo();
          // }
    
          for (int i = 0; i < bar.length; i++) {
             bar[i] = new Foo();
          }

  5. #5
    emerald000 is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Problem with an array of objects

    I thought the objects where getting initialized by calling the array. Is there a pretty way to do so, or only like this:

    Java Code:
    		for(int i=0;i<bar.length;i++) {
    			bar[i] = new Foo();
    		}
    EDIT: I guess Fubarable pretty much answered a second before I posted.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with an array of objects

    Only the array is initialized by your first code, not the items in the array. You can initialize it in other ways if desired:

    Java Code:
    Foo[] bar = {new Foo(), new Foo(), new Foo(), new Foo(), new Foo(), new Foo(), new Foo(), new Foo()};

  7. #7
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: Problem with an array of objects

    my apologies. Too much C# lateley

Similar Threads

  1. Array of objects
    By EnSlavingBlair in forum New To Java
    Replies: 4
    Last Post: 09-30-2011, 01:27 PM
  2. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  3. help with and array of objects
    By hoosierfan24 in forum New To Java
    Replies: 5
    Last Post: 01-23-2011, 02:45 AM
  4. Array of objects
    By Saletra in forum New To Java
    Replies: 2
    Last Post: 07-31-2010, 12:16 PM
  5. Array of Objects
    By bluefloyd8 in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 06:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •