• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Learning about objects

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain to me why in the output is saying null.






 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Im sorry the actual program is this



 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This dog's name has not been set yet, thus it's null.
 
alex lesta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please PostRealCode next time.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 27364
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I will suppress my every urge. But not this shameless plug:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic