Recently, I programmed the click() event for a Datagrid.
However, this event fires when I scroll the datagrid which
shouldn't be happening because I am not clicking the actual
contents of the datagrid. Inside the click() event, I retrieve the
contents of the itemselected but if the user is scrolling, there is
no item selected and consequently, an error pops up stating that
there is a null pointer for the click event. Is there any way to
differentiate between scrolling and selecting a datagrid so this
exception does not occur?
1. You should be listening for the "change" event, not the
click event. The change event fires when the grid's selection
changes. The click event fires whenever you click anywhere in the
grid component (even the scrollbar).
2. When handling the event, you must check whether
selectedItem is null. It's possible for the user to unselect an
item, so you must compare against null first. eg:
if (grid.selectedItem == null)
return; //Do nothing