jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals()" Watch "equals()" New topic


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();
Parent p1 = new Parent();
Parent p2 = null;
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

Joined: Dec 26, 2003
Posts: 26
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.
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

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
I agree. Here's the link: http://aspose.com/file-tools
subject: equals()
It's not a secret anymore!