-
16 Jan 2009 9:55 AM #1
Deselecting a DataListItem throws a NullPointerException
Deselecting a DataListItem throws a NullPointerException
Hi all !
I wrote the following piece of code to catch events from selected datalistitems
but unfortunatly deselecting the selected DataListItem throws a NullPointerException. The behaviour actually works and the item is deselected , but still this exception is throw.Code:DataList list = new DataList(); list.setBorders(false); DataListSelectionModel sm = new DataListSelectionModel(SelectionMode.SINGLE); list.setSelectionModel(sm); Listener<ComponentEvent> listener = new Listener<ComponentEvent>() {public void handleEvent(ComponentEvent ce) {}; list.addListener(Events.SelectionChange, listener);DataList l = (DataList) ce.component; EntitiesModel model = store.findModel("entityStep", l.getSelectedItem().getText()); (l.getSelectionModel()).deselect(l.getSelectedItem());}
Any ideas why ?
TIA,
Noggy
-
19 Jan 2009 2:04 AM #2
I found out that setting the DataListSelectionModel to locked using
invalidates the selections. But then i can't use a Listener to know which selection was clicked. Any ideas on how i could achieve this behaviour ?Code:setLocked(true);
TIA,
CN
-
19 Jan 2009 3:41 AM #3
The problem is within your code.
After you deselect an item the SelectionChange event is fired again and than there is no item selected ->
throws the NPE.Code:l.getSelectedItem().getText()
-
20 Jan 2009 6:20 AM #4
Hello Sven, and thanks for the reply.
Who is listening to this SelectionChange event that the the line
fires ?Code:l.getSelectionModel()).deselect(l.getSelectedItem()
If it was the listener I used, then I should be able to read a message on the second
system.out.println
UsingCode:Listener<ComponentEvent> listener = new Listener<ComponentEvent>() { public void handleEvent(ComponentEvent ce) { DataList l = (DataList) ce.component; if(l.getSelectedItem().equals(null)) { System.out.println("nothing is selected!!"); } EntitiesModel model = store.findModel("entityStep", l.getSelectedItem().getText()); Info.display("Selected ", model.getEntityStep()); (l.getSelectionModel()).deselect(l.getSelectedItem()); if(l.getSelectedItem().equals(null)) { System.out.println("nothing is selected!!"); } } }; list.addListener(Events.SelectionChange, listener);
instead ofCode:l.getSelectionModel().getSelectedItem().equals(null)
also doesn't work.Code:(l.getSelectedItem().equals(null))
How do i prevent this NPE ??
TIA,
Noggy
-
20 Jan 2009 6:22 AM #5
Code:if(l.getSelectedItem()==null) { System.out.println("nothing is selected!!"); }
-
20 Jan 2009 6:24 AM #6
The red lines are badCode:Code:Listener<ComponentEvent> listener = new Listener<ComponentEvent>() { public void handleEvent(ComponentEvent ce) { DataList l = (DataList) ce.component; if(l.getSelectedItem().equals(null)) { System.out.println("nothing is selected!!"); } EntitiesModel model = store.findModel("entityStep", l.getSelectedItem().getText()); Info.display("Selected ", model.getEntityStep()); (l.getSelectionModel()).deselect(l.getSelectedItem()); if(l.getSelectedItem().equals(null)) { System.out.println("nothing is selected!!"); } } }; list.addListener(Events.SelectionChange, listener);
-
20 Jan 2009 6:38 AM #7


Reply With Quote