Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8122859

TableView selection with arrow keys throws NullPointerException

XMLWordPrintable

      Hi,

      When the focus traversing reaches a TableView instance, without having any rows previously selected, an NullPointerException is thrown in three cases:
      1. hitting the left arrow key
      2. hitting the right arrow key
      3. hitting the up arrow key
      Hitting the down arrow key successfully selects the first row in the TableView instance, then all arrow keys work just fine.

      If a row has been previously selected, no exception is thrown.

      Here is an SSCCE:

      import javafx.application.Application;
      import javafx.beans.property.BooleanProperty;
      import javafx.beans.property.SimpleBooleanProperty;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.property.StringProperty;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.scene.Scene;
      import javafx.scene.control.Label;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableView;
      import javafx.scene.control.cell.*;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class Sandbox extends Application{


          @Override
          public void start(Stage stage) throws Exception {
              ObservableList<Person> persons = FXCollections.observableArrayList();
              persons.add(new Person("Sami", "Haddad", false));
              persons.add(new Person("Ahmed", "Hasan", true));
              persons.add(new Person("Rami", "Kassar", true));
              persons.add(new Person("Nehad", "Hamad", false));
              persons.add(new Person("Jamal", "Raei", true));
              persons.add(new Person("Ameer", "Raji", true));
              persons.add(new Person("Tahseen", "Muhsen", true));

              Label label = new Label("TableView Selection Bug");
              label.setFocusTraversable(true);

              TableView<Person> tableView = new TableView<>();
              tableView.setItems(persons);

              TableColumn<Person, String> colFirstName = new TableColumn<>("First Name");
              colFirstName.setCellValueFactory(p -> p.getValue().firstName);
              colFirstName.setCellFactory(TextFieldTableCell.forTableColumn());
              colFirstName.setOnEditCommit(event -> event.getTableView().getItems().get(event.getTablePosition().getRow()).firstName.set(event.getNewValue()));

              TableColumn<Person, String> colLastName = new TableColumn<>("Last Name");
              colLastName.setCellValueFactory(p -> p.getValue().lastName);
              colLastName.setCellFactory(TextFieldTableCell.forTableColumn());
              colLastName.setOnEditCommit(event -> event.getTableView().getItems().get(event.getTablePosition().getRow()).lastName.set(event.getNewValue()));

              TableColumn<Person, Boolean> colInvited = new TableColumn<>("Invited");
              colInvited.setCellValueFactory(p -> p.getValue().invited);
              colInvited.setCellFactory(javafx.scene.control.cell.CheckBoxTableCell.forTableColumn(colInvited));

              tableView.getColumns().addAll(colFirstName, colLastName, colInvited);

              stage.setTitle("TableView Selection Bug");
              stage.setScene(new Scene(new VBox(label,tableView)));
              stage.show();

              label.requestFocus();
          }

          class Person {
              public StringProperty firstName;
              public StringProperty lastName;
              public BooleanProperty invited;

              public Person() {
                  this.firstName = new SimpleStringProperty("");
                  this.lastName = new SimpleStringProperty("");
                  this.invited = new SimpleBooleanProperty(false);
              }

              public Person(String fname, String lname, boolean invited) {
                  this();
                  this.firstName.set(fname);
                  this.lastName.set(lname);
                  this.invited.set(invited);
              }
          }

          public static void main(String[] args) {
              launch(args);
          }
      }


      To reproduce the bug:
      1. hit the tab key, to traverse the focus to the TableView instance.
      2. try hitting the left arrow key, a NullPointerException should be thrown
      3. same thing for right and up arrow keys

            jgiles Jonathan Giles
            asulaimanjfx Anas H. Sulaiman (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: