Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

a highly recommended mock site: http://www.jiris.com/mock

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a question in it:
class Test001
{
int i;
public Test001(int i) { this.i = i; }
public String toString()
{
if(i == 0) return null;
else return "" + i;
}
public static void main(String[ ] args)
{
Test001 t1 = new Test001(0);
Test001 t2 = new Test001(2);
System.out.println(t1);
System.out.println(t2);
}
}
why does it compiled, but thrown an exception in Runtime.
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is as good as writing
System.out.print(null)
which gives compile error
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any method that returns a reference to Object can always return null.
How ever when the toString method is called in System.out.println() the method is getting called on null referrence and hence the run time error.
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pradeep Bhat,Sarma Lolla :
thank you for clearing.
by the way what do you think of this mock exam?
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test001
{
int i;
public Test001(int i) { this.i = i; }
public String toString()
{
if(i == 0) return null;
else return "" + i;
}
public static void main(String[ ] args)
{
Test001 t1 = new Test001(0);
Test001 t2 = new Test001(2);
System.out.println(t1.toString());
System.out.println(t2);
}
}
is this compiled without an error, and print "null" to standard output
[ February 18, 2003: Message edited by: Mellihoney Michael ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test001
{
int i;
public Test001(int i) { this.i = i; }
public String toString()
{
if(i == 0) return null;
else return "" + i;
}
public static void main(String[ ] args)
{
Test001 t1 = new Test001(0);
Test001 t2 = new Test001(2);
System.out.println(t1);
System.out.println(t2.toString( ));
}
}
compile successfully
runtime exception
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ERIC:
brother,it indicated above two code fragments are identical!

[ February 18, 2003: Message edited by: Mellihoney Michael ]
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not content with my understanding of what I see.
Why does System.out.println(t1) have a NullPointerException?


print
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 printedS
See Also:
Object.toString()


Now if I use:

I have no problem. It somehow seems that print (or println) isn't doing what the Javadoc says it's supposed to do.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here ,I did'nt understand
Why does System.out.println(t1) have a NullPointerException?
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is really a controversy one!
but the fact is that
it is compiled OK,and throws the Exception on RunTime!
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, this is a good site!
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(t1); // automagically becomes
System.out.println( String.valueOf(t1) );
The String.valueOf says...
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned
In our case t1 is not null so obj.toString() is called which is cleverly overridden.
The t1.toString() returns a null value...then System.out.println executes thinking it has a reference to a String. It points to nothing so...NullPointerException (Thrown when an application attempts to use null in a case where an object is required.)

[ February 19, 2003: Message edited by: Tom Adams ]
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
I guess the moral to the story is "When overriding Object.toString(), don't return null; return a String."
Taking a closer look at the stack trace:

So the error occured at Writer.write. I am guessing the signature is the one below, but I don't know. I don't have Writer.java (source file).


write
public void write(char[] cbuf)
throws IOException
Write an array of characters.
Parameters:
cbuf - Array of characters to be written
Throws:
IOException - If an I/O error occurs


Maybe it's trying to loop through a null array:
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A difficult question. Thanks. This will help with studying for the exam.
But, unfortunately, I still don't understand the answer to the question. Does this mean that a null String reference can be passed to the println() method, but a null literal may not be passed to the method?
Do I have to know the exact reason for the exam? Because, I don't know the exact reason for this.
Thanks.
My Study Notes
[ March 19, 2003: Message edited by: Karin Paola Illuminate ]
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following codes copied form the java source file may give you a clue what's happening:
1: public void println(Object x) {
synchronized (this) {
print(x);
newLine();
}
2: the write method which in turn calls the write method in Writer class
public void print(Object obj) {
write(String.valueOf(obj));
}
3: the obj passed in is Test001(0) which is of course not null, if the obj passed in was null, it will then printed out as null.
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
4: when calling obj.toString() as in step 3, it returns null as in this case. By calling str.length() in the write method, it will of course throw NullPointerException, since it is same as calling null.length() and length() is an instance method not a class method.
//write mehtod from Writer class
public void write(String str) throws IOException {
write(str, 0, str.length());
}

Hope this help, please forgive my poor English.
 
reply
    Bookmark Topic Watch Topic
  • New Topic