Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array of objects using constructors

 
Zac Lewis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have so many constructor parameters, which you appear never to use?
 
Zac Lewis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic