• 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

null exception!

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) );
}
}
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
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 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
 
Adam S-R
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this code:
String s1 = "";
System.out.println(s1.equals(null)); // prints false
 
Adam S-R
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you're welcome
Val
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
"Calling the instance method of a null object. "
Can somebody explains the meaning with a one line code.
Thanx
Rajani
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic