This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSF and the fly likes how to handle the situation if nothing selected from <h:selectOneMenu> and is permitted Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "how to handle the situation if nothing selected from <h:selectOneMenu> and is permitted " Watch "how to handle the situation if nothing selected from <h:selectOneMenu> and is permitted " New topic
Author

how to handle the situation if nothing selected from <h:selectOneMenu> and is permitted

michael nik
Greenhorn

Joined: Mar 11, 2012
Posts: 15
Hi, I'm retrieving some values from 5-6 <h:selectOneMenu> in my form, the user is able to not select values from all the <h:selectOneMenu> or in some cases a <h:selectOneMenu> should have the attribute disabled="true". In both of the cases, when the user submits the form, I got the same interval server error , and Null pointer Exception.

I tried to handle and catch the null values, or I tried to declare the attribute itemValue=" " in <h:selectOneMenu>, but I couldn't solve the problem,

Any ideas on how to avoid these exceptions of non-selected options of a <h:selectOneMenu>? or due to the disabled="true" of <h:selectOneMenu>?


Please I need help.
Thank you
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

There is no such thing as "not selected" in an HTML SELECTITEM (which is what backs the selectOneMenu). Something will be selected. And in the case of JSF, if the something isn't one of the SelectItem values, you'll fail with a Validation Exception.

My usual approach is this:


HTML is a text protocol, so if this control is submitted, it will be set to a non-null value. By setting "required=true", I told JSF to fail cases where no item was selected. If I had not added that constraint, the myBean selection value would be the empty string ("") if no selection had been made. Outbound, a null value will cause the first item on the list to be selected, so the value would go out as null and come back as "". Which is dicey, so you're better off pre-initializing the selection value to "", instead.


Customer surveys are for companies who didn't pay proper attention to begin with.
michael nik
Greenhorn

Joined: Mar 11, 2012
Posts: 15
Thank you for the reply Tim Holloway,

I found an answer for my problem in a similar way, thank you again for your useful advices.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to handle the situation if nothing selected from <h:selectOneMenu> and is permitted