PDA

View Full Version : Deselecting a DataListItem throws a NullPointerException



Noggy
16 Jan 2009, 9:55 AM
Hi all !

I wrote the following piece of code to catch events from selected datalistitems


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) {
DataList l = (DataList) ce.component;
EntitiesModel model = store.findModel("entityStep", l.getSelectedItem().getText());
(l.getSelectionModel()).deselect(l.getSelectedItem());
}
};

list.addListener(Events.SelectionChange, listener);

but unfortunatly deselecting the selected DataListItem throws a NullPointerException. The behaviour actually works and the item is deselected , but still this exception is throw.

Any ideas why ?

TIA,
Noggy

Noggy
19 Jan 2009, 2:04 AM
I found out that setting the DataListSelectionModel to locked using


setLocked(true);

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 ?

TIA,
CN

sven
19 Jan 2009, 3:41 AM
The problem is within your code.

After you deselect an item the SelectionChange event is fired again and than there is no item selected ->

l.getSelectedItem().getText()

throws the NPE.

Noggy
20 Jan 2009, 6:20 AM
Hello Sven, and thanks for the reply.

Who is listening to this SelectionChange event that the the line

l.getSelectionModel()).deselect(l.getSelectedItem()fires ?

If it was the listener I used, then I should be able to read a message on the second
system.out.println


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);



Using

l.getSelectionModel().getSelectedItem().equals(null)
instead of

(l.getSelectedItem().equals(null))
also doesn't work.

How do i prevent this NPE ??

TIA,
Noggy

sven
20 Jan 2009, 6:22 AM
if(l.getSelectedItem()==null) {
System.out.println("nothing is selected!!");
}

sven
20 Jan 2009, 6:24 AM
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);



The red lines are bad

Noggy
20 Jan 2009, 6:38 AM
Thanks.

It worked!