• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Correcting an array error

 
Greenhorn
Posts: 8
Android Netbeans IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Netbeans IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic