0

problem to get ID in listbox in onSelect Event

asked 2011-07-07 12:52:53 +0800

nilsonhp gravatar image nilsonhp
123 3

Hi, everyone.

I'm working in an apliccation, and i had the following code:

	@SuppressWarnings("unchecked")
	public void constructPhoneList(Set<Phone> list) throws Exception {

		Set<Phone> setPhone = (Set<Phone>) divListPhones
				.getAttribute("listphones");

		listBoxPhones.getChildren().clear();

		Listhead listhead = new Listhead();
		Listheader listheader = new Listheader("Código Área");
		listheader.setHflex("1");
		listheader.setAlign("left");
		listheader.setParent(listhead);
		listheader = new Listheader("Numero");
		listheader.setHflex("1");
		listheader.setParent(listhead);

		for (Phone phone : setPhone) {
			Listitem listitem = new Listitem();
			Listcell listcell = new Listcell(phone.getAreaCode());
			listcell.setParent(listitem);
			listcell = new Listcell(phone.getNumber());
			listcell.setParent(listitem);

			listitem.setValue(phone);
			listitem.setParent(listBoxPhones);
		}

		listBoxPhones.addEventListener("onSelect", new EventListener() {

			@Override
			public void onEvent(Event arg0) throws Exception {
				PhoneID = ((Phone) listBoxPhones.getSelectedItem().getValue())
						.getID();

			}
		});

		listhead.setParent(listBoxPhones);
	}

	@SuppressWarnings("unchecked")
	public void constructEmailList(Set<Email> list) throws Exception {

		Set<Email> setEmails = (Set<Email>) divListEmails
				.getAttribute("listemails");

		listBoxEmails.getChildren().clear();

		Listhead listhead = new Listhead();
		Listheader listheader = new Listheader("Endereço");
		listheader.setHflex("1");
		listheader.setAlign("left");
		listheader.setParent(listhead);
		listheader = new Listheader("Contato");
		listheader.setHflex("1");
		listheader.setParent(listhead);

		for (Email email : setEmails) {
			Listitem listitem = new Listitem();
			Listcell listcell = new Listcell(email.getAddress());
			listcell.setParent(listitem);
			listcell = new Listcell(email.getContact());
			listcell.setParent(listitem);

			listitem.setValue(email);
			listitem.setParent(listBoxEmails);
		}

		listBoxEmails.addEventListener("onSelect", new EventListener() {

			@Override
			public void onEvent(Event arg0) throws Exception {
				EmailID = ((Email) listBoxEmails.getSelectedItem().getValue())
						.getID();

			}
		});

		listhead.setParent(listBoxEmails);

	}

My problem is, when i try to get EmailID, it works, but when i try to get the value of phoneID, in the onSelect Event, he returns the followig exception:

Jul 7, 2011 2:51:24 PM org.zkoss.zk.ui.impl.UiEngineImpl handleError:1253
SEVERE: >>java.lang.NullPointerException
>>	at com.sac.persondata.ui.PersondataCardController$2.onEvent(PersondataCardController.java:234)
>>	at org.zkoss.zk.ui.impl.EventProcessor.process0(EventProcessor.java:206)
>>	at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:140)
>>	at org.zkoss.zk.ui.impl.UiEngineImpl.processEvent(UiEngineImpl.java:1602)
>>	at org.zkoss.zk.ui.impl.UiEngineImpl.process(UiEngineImpl.java:1386)
>>	at org.zkoss.zk.ui.impl.UiEngineImpl.execUpdate(UiEngineImpl.java:1106)
>>...

I have no idea about what's goin on.

both, email and phone, had the same methods and similar variables.

can somebody help me??

delete flag offensive retag edit

5 Replies

Sort by » oldest newest

answered 2011-07-07 13:23:05 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

The first thing I'd do is change onEvent(Event evt) for both of your listbox listeners. I'd look to see what real Event class is being passed. You should be able to change that to a SelectEvent. Using the SelectEvent, you can get the value of the selected list item and not have to use the getSelectedItem().getValue() scheme you're currently using.

Next, I'd break the onEvent code apart and see what each piece returns instead of a single statement such as ((Email) listBoxEmails.getSelectedItem().getValue()).getID().

link publish delete flag offensive edit

answered 2011-07-07 13:47:56 +0800

nilsonhp gravatar image nilsonhp
123 3

i'm not faliliar with the SelectEvent. If i understando, i have to create a method like this:

public void onSelect$listBoxPhones (SelectEvent event){
		....
		
		
	}
	

but, what i should use to get the ID of the selected object? i'm confuse about this.

thanks

link publish delete flag offensive edit

answered 2011-07-07 14:15:28 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

No, that method signature would only work if you were autowiring you event handlers. Since you're manually doing them, you still need your onEvent handler, but changed:

			public void onEvent(SelectEvent event) 
			    throws Exception
			{
				Component comp = event.getReference() ;
                                // see if the comp is a listitem or the listbox...if it's the listitem, get the value
                                //  if it's the listbox, use event.getSelectedItems() to get the Set of items and use the first (and likely only) one
			}
		});

link publish delete flag offensive edit

answered 2011-07-07 14:30:11 +0800

nilsonhp gravatar image nilsonhp
123 3

thanks, i'll try your idea.

link publish delete flag offensive edit

answered 2011-07-07 17:56:03 +0800

TonyQ gravatar image TonyQ
642
https://www.masterbranch....

The issue is not the event ,
you could get selectedItems on SelectEvent.getSelectedItems() (it's a set of selected list item),

then get the value from those items.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-07-07 12:52:53 +0800

Seen: 462 times

Last updated: Jul 07 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More