File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals()

 
Vineela Devi
Ranch Hand
Posts: 191
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use equality operators (== or !=) for checking null.
 
kuchi DASS
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic