• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Object Reference Variable question

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone,

I am currently reading through the 2nd edition of Head First Java. I finished the problems at the end of Chapter 3 and began to manipulate them, seeing what I could change while still yield the same output. My question pertains to the following two problems :

Problem 1 :


Problem 2 :


They both compile and run correctly. However, when I take : out of the while loop, in Problem 2, and place it under: the code does not compile? As apposed to the new objects created outside the while loop in Problem 1 which does compile?
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's because the compiler knows that h[-1] is illegal, but I could be wrong. No, it compiles for me. What is the message you get?
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:I think it's because the compiler knows that h[-1] is illegal, but I could be wrong. No, it compiles for me. What is the message you get?


If it compiles for you, Knute, I suspect it would compile for Renne, too. My guess is that he introduced a typo that he hasn't spotted yet.

Renne, please post the message the compiler is giving you, like Knute requested, but also post the full code that is not compiling.
 
Renne Bautista
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:

Knute Snortum wrote:I think it's because the compiler knows that h[-1] is illegal, but I could be wrong. No, it compiles for me. What is the message you get?


If it compiles for you, Knute, I suspect it would compile for Renne, too. My guess is that he introduced a typo that he hasn't spotted yet.

Renne, please post the message the compiler is giving you, like Knute requested, but also post the full code that is not compiling.



So I did find the error that was not allowing me to compile. After fixing it it now compiles, but when I attempt to run it I receive this error as follows :

Also, I am compiling and running through the terminal using bash.
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Renne, please post the full code that you are running. We can't help you if you don't.
 
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the array index starts at 0 and you cannot therefore store anything at position <0
 
Renne Bautista
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:Renne, please post the full code that you are running. We can't help you if you don't.




Here is the original code from the book :


and this is the one I've altered (all i did was place the object reference outside of the while loop) :
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I told you in my previous post ...
 
Sheriff
Posts: 9019
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Renne Bautista,

Apart from what has been mentioned already, there are more things to pay attention on:

1. Even tho, if this class is just for a test purpose, please keep in mind when you implement non-test, that instance variables in most cases should be private. Do differently if you have valid reason to do so.
2. Also, name variables so they'd reveal their purpose from the first glance. i.e. variables h, z are not descriptive and self explaining. Such things shouldn't be practiced even in test classes.
3. Don't really understand why you initialize z to -1? and in this matter using while loop which iterates in that way. Shouldn't be easier to use traditional form of for loop and iterate from 0 till ... ?
4. Each Hobbit you name as "bilbo", and later on if it turns out that it is 1 or 2 you rename it accordingly. That is also not very correct. Think of case how to name it once.
5. On lines 10 and 13 you have two if statements. Consider in such cases using else if. Bear in mind, it is going to be either 1 or 2, but never both.
6. Last thing. Try do not cram everything to a main method, as it suppose to start your application only.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic