Struts form doubt.

Hi,

    I have a <html:select> with multiple = "true" set.  I have a corresponding string[] array in the form to get populated with the values of the selected items. To know how many got selected i am writing this code in the action handler

mlForm.getIds().length  

if anything gets selected its running fine, if nothing is selected it gives a null pointer exception and i am not able to add the row to database... because of this exception.

suggestions please

Thanks
letsbedecentAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
TimYatesConnect With a Mentor Commented:
for( int i = 0 ; i < mlForm.getIds() == null ? 0 : mlForm.getIds().length ; i++ )
0
 
TimYatesCommented:
Or, if you want to do that in more than one line of code: ;-)

int len = 0 ;
if( mlForm.getIds() != null )
   len = mlForm.getIds().length ;
for( int i = 0 ; i < len ; i++ )
{
    // your insert code
}
0
 
letsbedecentAuthor Commented:
I will stick with the first one... thank you very much.

0
 
TimYatesCommented:
:-)

Basically, when you don't select anything on the form, nothing gets passed to your Form object so getIds() will return null...

That for statement will not loop if getIds() is null :-)

Good luck!

Tim
0
All Courses

From novice to tech pro — start learning today.