-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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??
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().
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
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 } });
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.
Asked: 2011-07-07 12:52:53 +0800
Seen: 462 times
Last updated: Jul 07 '11