aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals()" Watch "equals()" New topic
Author

equals()

Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
class Parent
{
public static void main(String[] args)
{Parent p = new Kid();
Class c = p.getClass();
System.out.println(c);
Parent p1 = new Parent();
Parent p2 = null;
System.out.println(p1.equals(p2));
System.out.println(p2.equals(null));// Line 1
}
}

class Kid extends Papa{
}

anyone please expalin why a null pointer exception is thrown at line 1 when i compile the above code
Rama Kumar PV
Greenhorn

Joined: Dec 26, 2003
Posts: 26
Vineela,
The reference variable P2 is not pointing any where as it is null. You cannot invoke methods on a null pointed reference variable as it will lead to NullPointerException as you got in this case.
Cheers,
Rama
Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
You can use equality operators (== or !=) for checking null.


Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)
kuchi DASS
Greenhorn

Joined: Sep 02, 2003
Posts: 6
Yes kumar is right. variable p2 has been assigned a null reference which means p2 is not holding reference to any object.
now equals is a method of Object class inherited my every other class when you perform p2.equals(null) u are calling equals method of class object where as p2 has not been assigned any reference as yet.
I hope its clear
Nilendu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals()