Struts form doubt.

Posted on 2005-04-21
Last Modified: 2010-03-31

    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


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

Question by:letsbedecent
    LVL 35

    Accepted Solution

    for( int i = 0 ; i < mlForm.getIds() == null ? 0 : mlForm.getIds().length ; i++ )
    LVL 35

    Expert Comment

    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

    Author Comment

    I will stick with the first one... thank you very much.

    LVL 35

    Expert Comment


    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!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    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…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now