1. #1
    Ext User
    Join Date
    Dec 2008
    Posts
    34
    Vote Rating
    0
    Noggy is on a distinguished road

      0  

    Question 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
    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) {
    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

  2. #2
    Ext User
    Join Date
    Dec 2008
    Posts
    34
    Vote Rating
    0
    Noggy is on a distinguished road

      0  

    Default


    I found out that setting the DataListSelectionModel to locked using

    Code:
    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

  3. #3
    Software Architect
    Join Date
    Sep 2007
    Posts
    13,716
    Vote Rating
    107
    sven is just really nice sven is just really nice sven is just really nice sven is just really nice

      0  

    Default


    The problem is within your code.

    After you deselect an item the SelectionChange event is fired again and than there is no item selected ->
    Code:
    l.getSelectedItem().getText()
    throws the NPE.

  4. #4
    Ext User
    Join Date
    Dec 2008
    Posts
    34
    Vote Rating
    0
    Noggy is on a distinguished road

      0  

    Default


    Hello Sven, and thanks for the reply.

    Who is listening to this SelectionChange event that the the line
    Code:
    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

    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);
    Using
    Code:
    l.getSelectionModel().getSelectedItem().equals(null)
    instead of
    Code:
    (l.getSelectedItem().equals(null))
    also doesn't work.

    How do i prevent this NPE ??

    TIA,
    Noggy

  5. #5
    Software Architect
    Join Date
    Sep 2007
    Posts
    13,716
    Vote Rating
    107
    sven is just really nice sven is just really nice sven is just really nice sven is just really nice

      0  

    Default


    Code:
    if(l.getSelectedItem()==null) {
      System.out.println("nothing is selected!!");
    }

  6. #6
    Software Architect
    Join Date
    Sep 2007
    Posts
    13,716
    Vote Rating
    107
    sven is just really nice sven is just really nice sven is just really nice sven is just really nice

      0  

    Default


    Code:
    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);
    The red lines are bad

  7. #7
    Ext User
    Join Date
    Dec 2008
    Posts
    34
    Vote Rating
    0
    Noggy is on a distinguished road

      0  

    Default


    Thanks.

    It worked!