• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Learning about objects

 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain to me why in the output is saying null.






 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got...

Bart says Ruff!
last dog's name is Bart
Fred says Ruff!
Marge says Ruff!
Bart says Ruff!


Henry
 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Im sorry the actual program is this


 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This dog's name has not been set yet, thus it's null.
 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:ok if you put dog name before dog1.bark() thats when you see the name instead of null.
This dog's name has not been set yet, thus it's null.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please PostRealCode next time.
 
Rob Hope
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I attempt to run the program I produce a print loop output of

Fredsays Ruff!
Fredsays Ruff!
Fredsays Ruff!
Fredsays Ruff!
Fredsays Ruff!


Unless i move the variable increment to above the call to .bark first.





But then the output errors to

nullsays Ruff!
last dog's name is Bart
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Dog.main(Dog.java:24)


My code appears to be the same as that shown above?
I removed an additional } found before the bark because I was getting error thrown otherwise
Dog.java:30: error: class, interface, or enum expected




My code
 
Paul Clapham
Sheriff
Posts: 21001
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "while" statement with its associated boolean calculated value controls the statement following it. In your example the while-statement at line 19 controls the statement at line 20. You probably thought that it also controls the statement at line 21, but it doesn't. If you want a while-statement to control a block of statements, you should surround them with {braces}.

You probably thought you had done that already because of the closing brace at line 22, but the matching opening brace for that one is way back at line 6. In fact you don't have matching braces in the posted code (3 opening braces, 4 closing braces) so it shouldn't even compile.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic