• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cannot pass checkbox array to Java bean

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good afternoon,

I am attempting to pass a checkbox array from a jsp page to a java bean with no luck. I am setting properties in the bean using the name property of the input tag. I realize this would be easier with Struts 2 or a similar framework but I am under serious time constraints with no time to spare learning a new framework.

It may be that the checkbox does not conform to an array type of Object or that I'm naming the input tag improperly. The initViewDVO() method is called from an earlier jsp page. Would someone be so kind as to peruse the following code snippets and, perhaps, provide me with a tip or two? Thank you.

Here is my bean:



The array is initialized here:



Here is the relevant jsp:

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jerry,

I know that if a group of check boxes is returned in a String []. When I use them in
a form, in my server/jsp code I do something like:



Then to iterate through:



Hope this helps.

John
 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,

Thank you for your reply. I changed my check box array to a type of String[] rather than Object[] however I still don't get the result I want. I am testing my result by displaying the first element of the array on my web page like so:



The initial value is displayed correctly but does not update when the form is submitted. I have also tried getting the check box using the request parameter you mentioned:



This results in a null pointer exception at the out.print... line.

Thanks again for taking the time to help. I will post when I figure this one out. I'm sure I am missing something simple.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jerry Goldsmith wrote:



Jerry Goldsmith wrote:



In your JSP, you are having a different name for your checkbox and in getParameterValue() you are using a different name.

 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John. Thanks Sunil.

Actually, the array name in the input tag and the request statement did match. That was just a typo.

I found the real issue. A checkbox array will return null, if nothing is checked, in which case you need to catch the null Pointer Exception. If it is checked, the array element will contain the value from the corresponding input tag. In my case that would be my iterator "i".

Thanks again.

Cheers!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic