• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

comparing null with null

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.
  • 4
  • 3
  • 2
  • +3
5 Solutions
Gurvinder Pal SinghCommented:
this should work as long as Object class has overridden hashCode and equals method
why don't you just use

boolean same = list1.equals(list2);
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

if you just say,
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.

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

PearlJamFanaticAuthor Commented:
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.

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?

Sathish David Kumar NArchitectCommented:

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
PearlJamFanaticAuthor Commented:
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.
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

> . 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 :)
Sathish David Kumar NArchitectCommented:
why u comapre with null ? you change that as ""(speace ) and comapre


while ur puting into array list jsut check each value like

arraylist.add(a!=null?a:"") ; then you can easy to comapre !!
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               }
  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       }
>>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
Sathish David Kumar NArchitectCommented:
>>>>>>>e1 == null ? e2 == null : e1.equals(e2)))

as per code if e1 null means it automatically take e2 is null is this correct ??
> 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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now