Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Does the toString() method cannot return null?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

compile ok, but it will throw a runtime exception. Does the toString() method cannot return null?
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try instead:

(removed incorrect interpretation of stack trace)
Cheers, Neil
[ November 14, 2002: Message edited by: Neil Laurance ]
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by the "wrong" version of println() ?
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

compile ok, but it will throw a runtime exception. Does the toString() method cannot return null
.................................................
how about making one small change to your return of the null(where i==0), which would be consistent with how you wrote the return where i==2, in the IF decision block within your toString definition, as follows:

[ November 13, 2002: Message edited by: david eberhardt ]
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kelvin,
String method can return null but the exception is thrown at line 1 where its trying to print null as print() method invoke toString() method of object class on its argument and invoking toString() method on null value gives NullPointerException.You can make the code run fine by making slight changes suggested by Neil and David.
Plz correct me if i am wrong.
regds
Arpana
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arpana Rai:
Kelvin,
String method can return null but the exception is thrown at line 1 where its trying to print null as print() method invoke toString() method of object class on its argument and invoking toString() method on null value gives NullPointerException.You can make the code run fine by making slight changes suggested by Neil and David.
Plz correct me if i am wrong.
regds
Arpana



Yes, excellent description of what is happening there !!
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's another idea on this topic:
I added another call to System.out.println at the end of the original code (which originally compiled but failed at runtime due to nullPointerException)

This time it did not even compile!
It has no idea what needs to be printed because there is ... nothing to print.
[ November 13, 2002: Message edited by: david eberhardt ]
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
take a look at PrintStream class :
public void print(String s)
Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the
platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
Parameters:
s - The String to be printed
public void print(Object obj)
Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default
character encoding, and these bytes are written in exactly the manner of the write(int) method.
Parameters:
obj - The Object to be printed
See Also:
Object.toString()
.....

you might be able to accomplish your goal, IF you had been trying to print the value of i or null, instead of invoking the print (Object obj) method ...
... by passing t2 into the println method, you invoke the print(Object obj) method signature.
 
Kelvin Mak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am clearly know this question, and appricated all the reply!!!
 
Arpana Rai
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right David.There is one more thing.If u call print/println() method with no arguments i.e
System.out.print() it will give error at the time of compliling b'coz no-argument print() method is not defined in PrintStream class.
regds
Arpana
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ November 13, 2002: Message edited by: david eberhardt ]
 
Arpana Rai
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right David, Thanx for the correction
regds
Arpana
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arpana Rai:
you are right David, Thanx for the correction
regds
Arpana


Thanks Arpana ... this was a very stimulating topic as I looked thru alot of docs and tried compiling and running many variations.
Thanks to all!
reply
    Bookmark Topic Watch Topic
  • New Topic