From 4c1919c6ec83ef60f44e688aafe83a2eaf25ef97 Mon Sep 17 00:00:00 2001 From: mlindfors Date: Mon, 4 Feb 2019 13:54:09 +0200 Subject: Fix an NPE caused by the previous fix to Grid's frozen columns (#11444) * Fix an NPE caused by the previous fix to Grid's frozen columns (#10653) If the user managed to get two frozen column count changes into a single server round-trip before the component had been attached, the previous fix #11346 lead to a null pointer exception since there was no diff state available. This fix adds a null check before removing the frozen column count value from the diff state so that this will not happen. Closes #10653 --- server/src/main/java/com/vaadin/ui/Grid.java | 8 ++- .../tests/components/grid/GridFrozenColumnNPE.java | 60 ++++++++++++++++++++++ .../components/grid/GridFrozenColumnNPETest.java | 26 ++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 uitest/src/main/java/com/vaadin/tests/components/grid/GridFrozenColumnNPE.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/grid/GridFrozenColumnNPETest.java diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index 650bf7cab5..9ee57512b2 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -3232,8 +3232,12 @@ public class Grid extends AbstractListing implements HasComponents, final String diffStateKey = "frozenColumnCount"; UI ui = getUI(); if (ui != null) { - ui.getConnectorTracker().getDiffState(Grid.this) - .remove(diffStateKey); + JsonObject diffState = ui.getConnectorTracker() + .getDiffState(Grid.this); + // if diffState is not present, there's nothing for us to clean + if (diffState != null) { + diffState.remove(diffStateKey); + } } } getState().frozenColumnCount = numberOfColumns; diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridFrozenColumnNPE.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridFrozenColumnNPE.java new file mode 100644 index 0000000000..1d7f5901f3 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridFrozenColumnNPE.java @@ -0,0 +1,60 @@ +package com.vaadin.tests.components.grid; + +import java.util.ArrayList; +import java.util.List; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.data.bean.Person; +import com.vaadin.ui.Button; +import com.vaadin.ui.Grid; +import com.vaadin.ui.renderers.NumberRenderer; + +public class GridFrozenColumnNPE extends SimpleGridUI { + + private Grid grid; + + @Override + protected void setup(VaadinRequest request) { + grid = new Grid(); + grid.setSizeFull(); + init(); + getLayout().addComponent(grid); + + grid.setFrozenColumnCount(1); + + Button button = new Button("change frozen count"); + button.addClickListener(event -> { + reInit(); + }); + getLayout().addComponent(button); + } + + @Override + protected List createPersons() { + List persons = new ArrayList<>(); + for (int i = 0; i < 10; ++i) { + Person person = new Person(); + person.setFirstName("First " + i); + person.setLastName("Last" + i); + person.setAge(i); + persons.add(person); + } + return persons; + } + + protected void init() { + grid.addColumn(Person::getFirstName); + grid.addColumn(Person::getLastName); + grid.addColumn(Person::getAge, new NumberRenderer()); + + grid.setItems(createPersons()); + grid.setFrozenColumnCount(1); + grid.setFrozenColumnCount(2); + } + + protected void reInit() { + grid.removeAllColumns(); + init(); + } + +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridFrozenColumnNPETest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridFrozenColumnNPETest.java new file mode 100644 index 0000000000..29b91d3326 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridFrozenColumnNPETest.java @@ -0,0 +1,26 @@ +package com.vaadin.tests.components.grid; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.GridElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class GridFrozenColumnNPETest extends MultiBrowserTest { + + @Test + public void testFrozenColumnNPE() { + openTestURL(); + GridElement grid = $(GridElement.class).first(); + + assertTrue(grid.getCell(0, 0).isFrozen()); + + ButtonElement button = $(ButtonElement.class).first(); + button.click(); + + assertTrue(grid.getCell(0, 1).isFrozen()); + } + +} -- cgit v1.2.3