aspose file tools*
The moose likes Beginning Java and the fly likes Array of objects using constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array of objects using constructors" Watch "Array of objects using constructors" New topic
Author

Array of objects using constructors

Zac Lewis
Greenhorn

Joined: Apr 23, 2012
Posts: 4
Sorry, I'm struggling for help even though this may sound a silly question..

These are myconstructors:

I have created an array of objects here:



The task: I am trying to display this information (TOY022, etc), but it displays a Null Pointer Exception error on this line:



Is there any other way to print this information? I think the error is not assigning default values to my variables, but what do I assign them to, because it acts as though the Toy objects at toy[j] seem to be ignored.

Please help. Thanks in advance.


Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

Well, you seem to have declared an array of size 10 in which you've stuffed 3 Toy objects.
What do you think happens when you iterate the array using its length property?
Will it stop after only three iterations?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi Zac, and welcome to the Ranch!

It looks like you're creating an array of 10 Toy references but only initializing the first 3. Array elements have default values of 0/false/null, depending on the element type.

So you have 3 main choices:

1. Create an array of only 3 elements.

2. Initialize all 10 elements.

3. Check for array[i] != null before printing it.

Note that #1 and #2 are really the same thing--don't create your array any bigger than you need.

Finally, when posting code, please UseCodeTags(⇐click) so it will be readable.
Zac Lewis
Greenhorn

Joined: Apr 23, 2012
Posts: 4
Of course, that makes so much sense now.
Except I ran into a problem compiling that: If I make it
toys = new Toy[3];
I still get the exact same error message.

But, the requirements of my task say "Create an array which can hold 10 objects, but only create 3 objects." So, is there a way to create "blank" arrays? Or would I just have to create a dynamic array (one which does not have a fixed length, if I am correct).
Thanks for the help.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Zac Lewis wrote:Of course, that makes so much sense now.
Except I ran into a problem compiling that: If I make it
toys = new Toy[3];
I still get the exact same error message.


Impossible to diagnose without an SSCCE.

But, the requirements of my task say "Create an array which can hold 10 objects, but only create 3 objects." So, is there a way to create "blank" arrays? Or would I just have to create a dynamic array (one which does not have a fixed length, if I am correct).
Thanks for the help.


Java arrays always have a fixed length. It would appear that part of the point of this assignment is to teach you how to deal with arrays that don't have all of their slots used. I already mentioned how to deal with that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Why do you have so many constructor parameters, which you appear never to use?
Zac Lewis
Greenhorn

Joined: Apr 23, 2012
Posts: 4
Alright, I made my code as brief as I can. I'm doing online study so there's not really anyone I can talk to about this.



This displays the letter 'a'. Can someone tell me why it seems to be ignoring the object? :S
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Zac Lewis wrote:
Can someone tell me why it seems to be ignoring the object? :S


Because you told it to.



The first line says, "This object will have a String member variable called toyID, and before any constructor is called, we will initialize it to "a" ".

The constructor says, "Accept a parameter called toyID, and totally ignore it, do nothing (except implicitly call the supserclass's constructor) and return".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Zac Lewis wrote:. . . If I change it to System.out.print(array1) the output is "[LExample;@4fe5e2c3". . . .
that is because you didn’t override the toString() method.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:
Zac Lewis wrote:. . . If I change it to System.out.print(array1) the output is "[LExample;@4fe5e2c3". . . .
that is because you didn’t override the toString() method.


Nor can you for an array.

You could, however, override toString() in Example, and have it return, for instance, the toyId. Then instead of printing out array1[0].toyId you could just print out array1[0].
Zac Lewis
Greenhorn

Joined: Apr 23, 2012
Posts: 4
Yes, I recognise now that it's just some empty constructor, doing nothing. The solution was so simple it's embarrassing:



Ta-da! It now prints the value. Sigh...

Thank you very much for your help all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array of objects using constructors