?
Solved

comparing two objects that have null values

Posted on 2003-03-02
7
Medium Priority
?
225 Views
Last Modified: 2010-03-31
is there away to use the .equals(function) when the calling object has a possibility that it would be null?

i.e.  if(a..equals(b))  //what if a = null;

as you know this will produce a NullPointerException.. but can is there another way to go around this?

0
Comment
Question by:c0upal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 8054619
something like:
if (a!=null && b!=null && a.equals(b))  

though it may depend on your requirements.
0
 
LVL 92

Expert Comment

by:objects
ID: 8054657
Or if you want to consider two nulls as equal then:

if ((a==null && b==null) || (a!=null && b!=null && a.equals(b)))  

0
 

Expert Comment

by:jaquo
ID: 8054729
Yeah, basically just test if the value that could possibly be null actually IS null before trying a compare.  Then you could add an else statement to handle it from there.

If (a != null) {
  if (a.equals(b))
    //what it is you want to do
}
else {
  //what to do it a is null
}


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:hellokns
ID: 8055629
Another simple solution can be helpful if you are sure that b is always not null, then have the statement like this,

if(b.equals(a)) {

}
In this case, even if a is null, u will not get nullpointer exception
0
 
LVL 92

Expert Comment

by:objects
ID: 8055648
> u will not get nullpointer exception

Not necessarily.
That depends on the implementation of the equals() method of b's class.
0
 

Expert Comment

by:CleanupPing
ID: 9058973
c0upal:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question