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

NullPointerException when sorting a list model bound to a Grid without a Columns component


      When sorting a list model that is bound to a Grid component that does not have a Columns child, Grid.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>() {
      public int compare(String o1, String o2)

      { return o1.compareTo(o2); }


      private ListModelList<String> listModel;

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

      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')"/>

      <grid id="grid" model="@bind(vm.listModel)">
      <template name="gridModel">
      <row><label value="@bind(each)"/></row>


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

      Apr 8, 2013 2:15:49 PM org.zkoss.zk.ui.impl.UiEngineImpl handleError:1359
      SEVERE: >>java.lang.NullPointerException
      >> at org.zkoss.zul.Grid.onListDataChange(Grid.java:951)
      >> at org.zkoss.zul.Grid.access$1400(Grid.java:211)
      >> at org.zkoss.zul.Grid$2.onChange(Grid.java:710)
      >> 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:41)
      [ Stack trace truncated ]

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

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

      Changes to:

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

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

            cor3000 cor3000
            tekkie tekkie
            0 Vote for this issue
            0 Start watching this issue