• Post Reply Bookmark Topic Watch Topic
  • New Topic

Correcting an array error  RSS feed

 
Michael Miller Jr
Greenhorn
Posts: 8
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Miller Jr
Greenhorn
Posts: 8
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Allan Kiplagat
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!