aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes null exception! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "null exception!" Watch "null exception!" New topic
Author

null exception!

ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
Why does the following code not throw a NullPointerException? Please also explain in what circumstances this exception is thrown!
public class MyClass {
public static void main(String args[]) {
String s1 = "Adventure";
String s2 = null;
System.out.println( s1.equals(s2) );
}
}
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Hi Ashok,
The reason why s1.equals(s2) when s2 is null is the following:
the equals method in the class String will accept an Object parameter. You pass on null and this is ok. Then the method performs the following test to see if the object passed as argument is a String:
if(obj instanceof String)
obj is here the parameter of the equals method (a copy of the reference to null you passed) and it is obvious that the test will fail and return false since null is not an instance of String.
Hope this helps
Val


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Adam S-R
Greenhorn

Joined: Aug 15, 2001
Posts: 18
Ashok
Check out this link to Sun's documentation at http://java.sun.com/j2se/1.3/docs/api/java/lang/NullPointerException.html . As for why you don't get a NullPointerException, I would think that
String s2 = null;
may actually create the string literal "", which would technically be an object and equals() could be used.
Anyone else add to/correct this?
Adam
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Adam S-R:
String s2 = null;
may actually create the string literal "", which would technically be an object and equals() could be used.
This is incorrect.
Try this:
String s1 = "";
String s2 = null;
System.out.println(s1.equals(s2)); // prints false
System.out.println(s2.equals(s1)); // throws NullPointerException
You can't run a method of a null pointer.

------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Adam S-R
Greenhorn

Joined: Aug 15, 2001
Posts: 18
Valentin
The reason why s1.equals(s2) when s2 is null is the following:
the equals method in the class String will accept an Object parameter. You pass on null and this is ok.

So u r saying that null can be used in place of an Object in equals()? How so, if AFAIK null isn't an Object? I am confused. Please clarify
Adam
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Adam,
I repeat the code given by Thomas for convenience:
String s1 = "";
String s2 = null;
System.out.println(s1.equals(s2)); //1. prints false
System.out.println(s2.equals(s1)); //2. throws NullPointerException
why line 2 throws an Exception is obvious so I won't come back on this.
line 1 is perfectly valid since the compiler has no way to know at compile time if the reference that will be given as argument to equals will be null or not. Then it has to accept the code as valid. The job is to be done by the equals method which has to make sure that it is not using an invalid parameter and thus has to make a test on it (obj instanceof String)...
I mean the bottom line is that if an argument has to be of type Object, it is perfectly legal to pass a null reference...
clearer now ?
Val
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Try this code:
String s1 = "";
System.out.println(s1.equals(null)); // prints false
Adam S-R
Greenhorn

Joined: Aug 15, 2001
Posts: 18
So u r saying that null can be used in place of an Object in equals()? How so, if AFAIK null isn't an Object? I am confused. Please clarify

Im going to answer my own question - similar to string literal "Adventure", null is null literal so can be used. Excuse my clouded thinking, I'm having a slow day.
Adam
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Adam,
Yes but be aware of what the following code may produce
public class Test {
public static void main(String args[]){
String s1 = "null";
String s2 = null;
System.out.println("s1="+s1);
System.out.println("s2="+s2);
System.out.println(s1.equals(s2));
}
}
Val
Adam S-R
Greenhorn

Joined: Aug 15, 2001
Posts: 18
Val
gives
null
null
false
Highlights that s2 is null "value" and that s1 is string literal with "null" value.
Many thanks
Adam
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
you're welcome
Val
Mini Pilla
Ranch Hand

Joined: Jul 15, 2001
Posts: 112
hi,
"Calling the instance method of a null object. "
Can somebody explains the meaning with a one line code.
Thanx
Rajani
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Hi Rajani,
"Calling the instance method of a null object. "
means that if you do the following:
String s = null;
s.trim(); // or any other method of the String class
you'll get a NullPointerException at runtime since s is a variable of type String referencing null. You can't invoke a method on null. This does not make any sense.
In fact the sentence should be rephrased like this
"Calling the instance method ON a null reference"
Val
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null exception!