• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jiris Mock 1, Q1

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

Why does this code throw an exception at runtime?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cathy Song:
Why does this code throw an exception at runtime?

Ultimately this null String gets passed into the write method of Writer which assumes that the String is not null. It runs the getChars method of the String which is where the exception occurs. Again, this is another case where reading through the source and following the stack trace can help you understand the API better.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cathy Song:

Why does this code throw an exception at runtime?


hello,
I suppose that the return type String is expected and null is returned if we return "null" it works - no runtime exception is thrown.
just make few changes in toString method and it works....
public String toString(){
if(i==0)
return "null";
else
return ""+i;
}
pls correct me am wrong....
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code did not throw any exception for me and the output was 2 and null.
I compiled with jdk1.4
class Class2
{
int i;
public Class2(int i) { this.i = i; }
public String toString()
{
if(i == 0) return null;
else return ""+i;
}
public static void main(String[ ] args)
{
Class2 t1 = new Class2(0);
Class2 t2 = new Class2(2);
System.out.println(t2);
System.out.println(""+t1);
}
}
Any thoughts?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a known JVM bug ID=4339222 (Synopsis: System.out.println throws NullPointerException when toString() returns null).
Workaround: Subclass and override toString, avoid to System.out.println.
Login to Bug Database for a full description.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Following expression works as well.
public String toString()
{
if(i == 0)
return "" + null;
else
return "" + i;
}

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

But explicitly calling "toString()" method works (jdk1.4). Wierd.
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic