Forums Register Login

Correcting an array error

+Pie Number of slices to send: Send
In the book, "Head First Java" (see Chapter 3), there is a section at the end of the chapter that tests the reader on various concepts. One is where you have to pretend to be the compiler and see if there are any errors in the proposed program and correct it...

The following is presented in the book:


This was my attempted correction:


The book (which I don't contend) gave this as the solution, however:


My question is if my proposed solution would also work. If not, what am I missing? I understand that in Java that there are multiple ways to do even the easiest and simplest of tasks as long as they obey the rules of Java...is my solution one of those ways?

THANKS!
1
+Pie Number of slices to send: Send
Did you try compiling and running your code? I didn't, but I'd imagine the following would happen:

Line 3: You create a new Hobbits array with 3 elements (with indices 0, 1 and 2).
Line 4: You declare a variable z which is set to 0.
Line 6: We enter the loop with z == 0.

Line 8: We set h[0] to a new Hobbits object.
Line 9: We set the "name" member variable of h[0] to "bilbo".

Lines 11 - 16: The expressions in the if-statements evaluate to false, so this is skipped.

Line 17: We increment z to 1.

Line 18: We print h[1].name.

NOTE: We did not yet set h[1] (remember, in line 8 we set h[0]). h[1] is still null, so trying to access the "name" member variable of h[1] will lead to a NullPointerException.
+Pie Number of slices to send: Send
 

Jesper de Jong wrote:Line 17: We increment z to 1.
Line 18: We print h[1].name.
NOTE: We did not yet set h[1] (remember, in line 8 we set h[0]). h[1] is still null, so trying to access the "name" member variable of h[1] will lead to a NullPointerException.



Doh! And therein rests my problem.
I should have written it like this:


...and you know what? In my notes, I actually did put z = z + 1; after the System.out.print statements. I created an error inadvertently when I typed it up here. Thank you for helping me to work that out.

I haven't actually plugged anything into the computer yet. I'm just following along with the book and we haven't gotten to the part of actually writing our own code and compiling anything. Though that would probably be a good idea to do anyway.
+Pie Number of slices to send: Send
 

I haven't actually plugged anything into the computer yet. I'm just following along with the book and we haven't gotten to the part of actually writing our own code and compiling anything. Though that would probably be a good idea to do anyway.



Yeah, it's a good idea to do that. The compiler will spot overlooked problems...for instance, the 'name' String variable hasn't been declared in the code.
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 569 times.
Similar Threads
What´s the difference?
Object Reference Variable question
Be the compiler excercise in chap 3
Array Out of Bounds Exception
Error in compiling
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:45:03.