Thanks Martin,

The method won’t error out on null values. The first part of the method
returns a false if either obj1 or obj2 are null.

I’d already isolated the code. The problem I’m having is that the Array
containing my checkbox selected values would occasionally contain a  null
value in the array (this was from a legacy system and I couldn’t easily do
anything with it).

In the end I added a workaround by iterating through the array and
discarding null values.

I still think the equals code for arrays should be the other way around as
it is for Iterable. That way it won’t throw an error.

The problem is purely in the way the Array equals code is written and if
changed won’t throw errors.

Z.
> 
> here is the code:
>     public static boolean contains(Object obj1, Object obj2) {
>         if ((obj1 == null) || (obj2 == null)) {
>             //log.debug("obj1 or obj2 are null.");
>             return false;
>         }
> 
>         if (obj1 instanceof Map) {
>             if (((Map) obj1).containsKey(obj2)) {
>                 //log.debug("obj1 is a map and contains obj2");
>                 return true;
>             }
>         } if (obj1 instanceof Iterable) {
>             Iterator iter = ((Iterable) obj1).iterator();
>             while(iter.hasNext()) {
>                 Object value = iter.next();
>                 if (obj2.equals(value) || obj2.toString().equals(value)) {
>                     return true;
>                 }
>             }
>         } else if (obj1.getClass().isArray()) {
>             for (int i = 0; i < Array.getLength(obj1); i++) {
>                 Object value = null;
>                 value = Array.get(obj1, i);
> 
>                 if (value.equals(obj2)) {
>                     //log.debug("obj1 is an array and contains obj2");
>                     return true;
>                 }
>             }
>         } 
> 
> //the Utility contains method will error out with
> null obj1
> null obj2
> obj1 which is not a valid Array
> obj2 which is not a valid Array
> 
> checkboxlist references required list attribute to draw from to quote
> list is a Iterable source to populate from. If the list is a Map (key, value),
> the Map key will become the option 'value' parameter and the Map value will
> become the option body.
> 
> http://struts.apache.org/2.0.14/docs/checkboxlist.html
> Martin Gainty 
> ______________________________________________
> Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
>  
> Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger
> sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung
> oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich
> dem Austausch von Informationen und entfaltet keine rechtliche
> Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen
> wir keine Haftung fuer den Inhalt uebernehmen.
> Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le
> destinataire prévu, nous te demandons avec bonté que pour satisfaire informez
> l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est
> interdite. Ce message sert à l'information seulement et n'aura pas n'importe
> quel effet légalement obligatoire. Étant donné que les email peuvent
> facilement être sujets à la manipulation, nous ne pouvons accepter aucune
> responsabilité pour le contenu fourni.
> 
> 
> 
> 
> 
>> > Date: Mon, 22 Jun 2009 22:14:31 +1000
>> > Subject: Freemarker error generated with CheckBox List
>> > From: zo...@sparecreative.com
>> > To: user@struts.apache.org
>> > 
>> > I¹m getting  a freemarker error when I use a s:checkboxlist tag and the
>> > array which contains my item values is empty.
>> > 
>> > When I had a look at the stack trace it¹s pointing me to the following
>> > error:
>> > 
>> > Caused by: java.lang.NullPointerException
>> >         at 
>> org.apache.struts2.util.ContainUtil.contains(ContainUtil.java:96)
>> > 
>> > When I had a look at the source for ContainUtil, I noticed that the equals
>> > expression is arse about.
>> > 
>> > if (obj1.getClass().isArray()) {
>> >             for (int i = 0; i < Array.getLength(obj1); i++) {
>> >                 Object value = null;
>> >                 value = Array.get(obj1, i);
>> > 
>> > (Error is thrown here)  if (value.equals(obj2)) {
>> >                     //log.debug("obj1 is an array and contains obj2");
>> >                     return true;
>> >                 }
>> >             }
>> > 
>> > Ideally the expression would be best rewritten if(obj2.equals(value))  as
>> we
>> > have already tested obj2 for nullness at the start of the contains method.
>> > 
>> > Now the hard part, I don¹t want to have to recompile the struts code and >>
was
>> > hoping there was a simple workaround.
>> > 
>> > All suggestions considered at this point.
>> > 
>> > TIA 
>> > 
>> > Z.
> 
> 
> Lauren found her dream laptop. Find the PC that’s right for you.
> <http://www.microsoft.com/windows/choosepc/?ocid=ftp_val_wl_290>


Reply via email to