Uploaded image for project: 'ZK'
  1. ZK
  2. ZK-1705

NullPointerException when sorting a list model bound to a Listbox without a Listhead component

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Normal Normal
    • 6.5.3
    • 6.5.2
    • Components
    • None

      When sorting a list model that is bound to a Listbox component that does not have a Listhead child, Listbox.onListDataChange(...) throws a NullPointerException.

      Consider this simple view model:

      package org.zkoss.testing;

      import java.util.ArrayList;
      import java.util.Comparator;

      import org.zkoss.bind.annotation.Command;
      import org.zkoss.bind.annotation.Init;
      import org.zkoss.zkplus.databind.BindingListModelList;
      import org.zkoss.zul.ListModelList;

      public class SimpleViewModel {

      private static final Comparator<String> COMPARATOR = new Comparator<String>() {
      @Override
      public int compare(String o1, String o2)

      { return o1.compareTo(o2); }

      };

      private ListModelList<String> listModel;

      @Init
      public void init()

      { listModel = new BindingListModelList<String>(new ArrayList<String>(), false); listModel.add("ONE"); listModel.add("TWO"); listModel.add("THREE"); }

      public ListModelList<String> getListModel()

      { return listModel; }

      public void setListModel(ListModelList<String> listModel)

      { this.listModel = listModel; }

      @Command
      public void sortList()

      { listModel.sort(COMPARATOR, true); }

      }

      Bound to this ZUL:

      <vbox apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('org.zkoss.testing.SimpleViewModel')">

      <button label="Sort Values" onClick="@command('sortList')"/>

      <listbox id="listbox" model="@bind(vm.listModel)">
      <template name="listboxModel">
      <listitem><listcell value="@bind(each)"/></listitem>
      </template>
      </listbox>

      </vbox>

      When the sortList() command is executed, the listbox throws a NullPointerException:

      Apr 8, 2013 2:27:35 PM org.zkoss.bind.impl.BinderImpl$CommandEventListener onEvent:1327
      SEVERE: null
      java.lang.NullPointerException
      at org.zkoss.zul.Listbox.onListDataChange(Listbox.java:2553)
      at org.zkoss.zul.Listbox.access$800(Listbox.java:261)
      at org.zkoss.zul.Listbox$4.onChange(Listbox.java:461)
      at org.zkoss.zul.AbstractListModel.fireEvent(AbstractListModel.java:59)
      at org.zkoss.zul.ListModelList.sort(ListModelList.java:417)
      at org.zkoss.testing.SimpleViewModel.sortList(SimpleViewModel.java:40)
      ...
      [ Stack trace truncated ]

      The fix seems as easy as just adding a null-pointer check to the enclosing conditional in Listbox.onListDataChange at line 2550:

      if (event.getType() == ListDataEvent.STRUCTURE_CHANGED
      && _model instanceof Sortable) {

      Changes to:

      if (event.getType() == ListDataEvent.STRUCTURE_CHANGED
      && _model instanceof Sortable
      && _listhead != null) {

      A workaround is to add the Listhead component with visibility set to false.

        1. index.zul
          0.3 kB
          tekkie
        2. SimpleViewModel.java
          1.0 kB
          tekkie

            cor3000 cor3000
            tekkie tekkie
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: