ZK - Open Source Ajax Java FrameworkZK - Open Source Ajax Java Framework

Checking Null Values of Radiogroup and combobox

swapndipj
9 Feb 2012 06:18:30 GMT
9 Feb 2012 06:18:30 GMT

Hello Everyone,

Need help on checking null values of radiogroup and combobox items?

Thanks
Swapnil

DaniG
9 Feb 2012 11:30:34 GMT
9 Feb 2012 11:30:34 GMT

Hi Swapnil,

Better if you specify a little more about your problem or give a little example about how you implement it. Anyway... I control my zul page with a class which extends GenericForwardComposer. To avoid nullPointerExceptions you can to put a default element as selected, maybe to check previously with component.getItemCount(), or to be sure you have the value attribute before to do a component.getValue()...

For example, to load my combos I use something like:

	for(MyOwnModel myModel : myList)
	{
		myComboItem = new Comboitem();
		myComboItem.setLabel ( myModel.getNameToShow() );
		myComboItem.setValue ( myModel.getNameValue() );
		myCombo.appendChild ( myComboItem );
	}

	comboTipoNum.setSelectedIndex(0);

Hope it helps.

Regards

swapndipj
13 Feb 2012 08:40:46 GMT
13 Feb 2012 08:40:46 GMT

Hi DaniG,

Appologies for late reply, I have a form in ZUL file with Combobox and radio group for filling the form. Now i want to keep these combobox and radiogroup component data as a optional for this form.But when i dont select any value for combobox and radiogroup and click on save, then it goes to composer in which i am selecting the value from these combobox for checking null values as (comboboox.getselecteditem.getlalabel();). it gives me null exceptions.

Please suggest any solutions for this..


Thanks in advance

Regard
Swapnil

DaniG
13 Feb 2012 10:17:05 GMT
13 Feb 2012 10:17:05 GMT

Hi Swapnil,

Yes, I know what you mean. ZK is different (to javascript for example). When you have a Combobox with no selection or default value it returns null, not blank ( "" ). I do:

	myComboItem = new Comboitem();
	myComboItem.setLabel ( " " );
	myComboItem.setValue ( "" );
	myCombo.appendChild ( myComboItem );

	for(MyOwnModel myModel : myList)
	{
		myComboItem = new Comboitem();
		myComboItem.setLabel ( myModel.getNameToShow() );
		myComboItem.setValue ( myModel.getNameValue() );
		myCombo.appendChild ( myComboItem );
	}

	comboTipoNum.setSelectedIndex(0);

for the "blank" element of my Combobox, and select it as default to prevent the null value. And if you have Comboitems I would suggest you use better the value attribute when you can:

		myComboItem.setLabel ( myModel.getNameToShow() );
		myComboItem.setValue ( myModel.getNameValue() );

When it is the same as label then:

		myComboItem.setLabel ( myModel.getNameToShow() );
		myComboItem.setValue ( myModel.getNameToShow() );


and to get it with myCombo.getSelectedItem().getValue();

Hope it helps you.

Regards