Skip to content

Commit 5843910

Browse files
Maran23aghaisas
authored andcommittedMay 21, 2021
8267392: ENTER key press on editable TableView throws NPE
Reviewed-by: fastegal, aghaisas
1 parent 111bac4 commit 5843910

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed
 

‎modules/javafx.controls/src/main/java/com/sun/javafx/scene/control/behavior/TableViewBehaviorBase.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -891,15 +891,20 @@ protected void activate(KeyEvent e) {
891891
if (fm == null) return;
892892

893893
TablePositionBase<TC> cell = getFocusedCell();
894-
sm.select(cell.getRow(), cell.getTableColumn());
894+
TC tableColumn = cell.getTableColumn();
895+
sm.select(cell.getRow(), tableColumn);
895896
setAnchor(cell);
896897

898+
if (tableColumn == null) {
899+
return;
900+
}
901+
897902
// check if we are editable
898-
boolean isEditable = isControlEditable() && cell.getTableColumn().isEditable();
903+
boolean isEditable = isControlEditable() && tableColumn.isEditable();
899904

900905
// edit this row also
901906
if (isEditable && cell.getRow() >= 0) {
902-
editCell(cell.getRow(), cell.getTableColumn());
907+
editCell(cell.getRow(), tableColumn);
903908
e.consume();
904909
}
905910
}

‎modules/javafx.controls/src/test/java/test/javafx/scene/control/TableViewKeyInputTest.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -63,6 +63,7 @@
6363
import org.junit.Test;
6464
import static org.junit.Assert.assertEquals;
6565
import static org.junit.Assert.assertFalse;
66+
import static org.junit.Assert.assertNotNull;
6667
import static org.junit.Assert.assertNotSame;
6768
import static org.junit.Assert.assertNull;
6869
import static org.junit.Assert.assertTrue;
@@ -184,6 +185,19 @@ private boolean isAnchor(int row, int col) {
184185
* Tests for row-based single selection
185186
**************************************************************************/
186187

188+
@Test
189+
public void testEnterOnFocusedRowDoesNotThrowNPE() {
190+
tableView.setEditable(true);
191+
192+
assertNull(tableView.getSelectionModel().getSelectedItem());
193+
assertEquals(0, tableView.getFocusModel().getFocusedCell().getRow());
194+
195+
// Fire an ENTER event on the focused row. This should not throw a NPE!
196+
keyboard.doKeyPress(KeyCode.ENTER);
197+
198+
assertNotNull(tableView.getSelectionModel().getSelectedItem());
199+
}
200+
187201
@Test public void testDownArrowChangesSelection() {
188202
sm.clearAndSelect(0);
189203
keyboard.doDownArrowPress();

‎modules/javafx.controls/src/test/java/test/javafx/scene/control/TreeTableViewKeyInputTest.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -62,6 +62,7 @@
6262
import org.junit.Test;
6363
import static org.junit.Assert.assertEquals;
6464
import static org.junit.Assert.assertFalse;
65+
import static org.junit.Assert.assertNotNull;
6566
import static org.junit.Assert.assertNotSame;
6667
import static org.junit.Assert.assertNull;
6768
import static org.junit.Assert.assertTrue;
@@ -242,6 +243,19 @@ private int getItemCount() {
242243
* Tests for row-based single selection
243244
**************************************************************************/
244245

246+
@Test
247+
public void testEnterOnFocusedRowDoesNotThrowNPE() {
248+
tableView.setEditable(true);
249+
250+
assertNull(tableView.getSelectionModel().getSelectedItem());
251+
assertEquals(0, tableView.getFocusModel().getFocusedCell().getRow());
252+
253+
// Fire an ENTER event on the focused row. This should not throw a NPE!
254+
keyboard.doKeyPress(KeyCode.ENTER);
255+
256+
assertNotNull(tableView.getSelectionModel().getSelectedItem());
257+
}
258+
245259
@Test public void testDownArrowChangesSelection() {
246260
sm.clearAndSelect(0);
247261
keyboard.doDownArrowPress();

0 commit comments

Comments
 (0)
Failed to load comments.