aspose file tools*
The moose likes Beginning Java and the fly likes Learning about objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Learning about objects" Watch "Learning about objects" New topic
Author

Learning about objects

alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
Can someone explain to me why in the output is saying null.






Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19002
    
  40

I got...

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


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
Henry Wong wrote:Im sorry the actual program is this


Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16


This dog's name has not been set yet, thus it's null.


[My Blog]
All roads lead to JavaRanch
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

Please PostRealCode next time.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Hope
Ranch Hand

Joined: Mar 31, 2012
Posts: 42
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
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Learning about objects