?
Solved

comparing null with null

Posted on 2011-03-13
14
Medium Priority
?
420 Views
Last Modified: 2012-05-11
I have the following scenario
1) i read values off a db table (recordset) to a arraylist
2) Same query is run on a different version of DB onto a another arraylist
3) the 2 array lists are comparaed value by value

Now the db table contains null values which get stored in the arraylist. When the values are compared (arraylist1.get(n).equals(arraylist2.get(n))) i get nullpointexception.

I found a workaround online

 public static boolean equals(Object a, Object b) {  
             return a==null ? b==null : a.equals(b);  
             }

and am currently using equals(arraylist1, arraylist2) for comparision. Is this a foolproof workaround? Please suggest.
0
Comment
Question by:PearlJamFanatic
[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
  • 4
  • 3
  • 2
  • +3
14 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 400 total points
ID: 35125688
this should work as long as Object class has overridden hashCode and equals method
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 35125692
why don't you just use

boolean same = list1.equals(list2);
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 400 total points
ID: 35125694
Seems like a good method, but I'm a little bit comfused are you using it to caompare
ArrayList's object by object or you are using it to compare objects within the list?

I was also thinking that perhaps method of class Object

public boolean equals(Object obj)  

was implemented fully in ArrayList so that
perhaps

if you just say,
 arraylist1.equals(arraylist2)
maybe it will perform comparison one by one even checking for the nulls for you and return the required
booolean value staisfying your neds - have you tried it?  Never tried it myelf  - just a thought.




0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:PearlJamFanatic
ID: 35125704
i think i made a mistake above. i am using
equals(arraylist1.get(n), arraylist2.get(n))
where arraylist2.get(n) is a string. I was just wondering if this will work in the scenario  arraylist1.get(n) is null and arraylist2.get(n) is a string and vice versa.

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35125736
In general it seems that your method should work because if the
first object is null, then you just check if the second object is null, however
if the fiorst object is not null, then your method a.equals(b) should not throw exception
and shoul yield false even if b is null - so I think method is fine.

On the other hand if your ArrayList's contain only objects of the types defined
by java libraries, like String, etc. or even if your ArrayLists contain custom objects
for which you overrode correctly the equals method, then this much more simple suggestion
arraylist1.equals(arraylist2) should most probably work even better.

Do your arraylists contain custom objects of the types which you defined yourself?


0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 400 total points
ID: 35125921
@PearlJamFanatic:

I have a question why you need null values ? any reason behind this ?

You can avoid null values in query itself

you can add where condition is not null

example : that is role coum means select * from user  where role is  not null
0
 

Author Comment

by:PearlJamFanatic
ID: 35126581
Arraylist does not contain customm objects. It contains int, string and date. The reason I went for value by value comparision is that I need to know which item (row, column) didn't match.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 35126613
The best thing is probably to initialize the object anyway. Assuming all three fields are involved in equals (they probably aren't) then if the query returns null, the below should allow the object to behave properly:
X x = new X(Integer.MIN_VALUE, "null", new Date(Long.MAX_VALUE));

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 35126629
> . I was just wondering if this will work in the scenario  arraylist1.get(n) is null and arraylist2.get(n) is a string and vice versa.

yes, passing null to equals() will work fine
you should actually write unit tests to check it :)
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35126639
why u comapre with null ? you change that as ""(speace ) and comapre

eg:

while ur puting into array list jsut check each value like

arraylist.add(a!=null?a:"") ; then you can easy to comapre !!
 
0
 
LVL 92

Expert Comment

by:objects
ID: 35126640
its the same method used by ArrayList.equals()


  480       public boolean equals(Object object) {
  481           if (this == object) {
  482               return true;
  483           }
  484           if (object instanceof List) {
  485               List<?> list = (List<?>) object;
  486               if (list.size() != size()) {
  487                   return false;
  488               }
  489  
  490               Iterator<?> it1 = iterator(), it2 = list.iterator();
  491               while (it1.hasNext()) {
  492                   Object e1 = it1.next(), e2 = it2.next();
  493                   if (!(e1 == null ? e2 == null : e1.equals(e2))) {
  494                       return false;
  495                   }
  496               }
  497               return true;
  498           }
  499           return false;
  500       }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35126687
>>why u comapre with null ? you change that as ""(speace ) and comapre

Yes, that's the  same principle as my suggestion, but it might be necessary to treat other fields too
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35126743
>>>>>>>e1 == null ? e2 == null : e1.equals(e2)))

as per code if e1 null means it automatically take e2 is null is this correct ??
0
 
LVL 92

Expert Comment

by:objects
ID: 35126759
> as per code if e1 null means it automatically take e2 is null is this correct ??

no if e1 is null, it will return whether e2 is null
ie. if e1 and e2 are null then return true
if only e1 is null then return false
otherwise call equals

works fine, no need to insert dummy values in the array. They only result in conversion and are error prone.
0

Featured Post

Technology Partners: 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

771 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