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

[TabPane] Tab.setContextMenu(null) throws NPE when the tab is not assigned to a TabPane (regression from 8)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 8u40
    • 8u40
    • javafx
    • U:\>java -version
      java version "1.8.0_40-ea"
      Java(TM) SE Runtime Environment (build 1.8.0_40-ea-b06)
      Java HotSpot(TM) 64-Bit Server VM (build 25.40-b10, mixed mode)

      I am debugging some application code where a Tab is set up with a context menu. At a certain point, the Tab is removed from its TabPane and afterwards the context menu is reset to null. This throws a NullPointerException, which is a bit fragile. I have created a small test application which triggers the same exception:

      Caused by: java.lang.NullPointerException
      at com.sun.javafx.scene.control.ControlAcceleratorSupport.removeAcceleratorsFromScene(ControlAcceleratorSupport.java:191)
      at javafx.scene.control.Tab$3.invalidated(Tab.java:396)
      at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
      at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
      at javafx.scene.control.Tab.setContextMenu(Tab.java:374)
      at ContextMenuTest.start(ContextMenuTest.java:27)

      Here is the test app:

      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ContextMenu;
      import javafx.scene.control.MenuItem;
      import javafx.scene.control.Tab;
      import javafx.stage.Stage;

      public class ContextMenuTest extends Application {

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

        @Override
        public void start(Stage stage) {
      // for a button it is OK to set the context menu then nullify it
          Button b1 = new Button("B1");
          b1.setContextMenu(createContextMenu("B1"));
          b1.setContextMenu(null);
          
      // but for a tab it throws an exception
          Tab t1 = new Tab("T1");
          t1.setContextMenu(createContextMenu("T1"));
          t1.setContextMenu(null);
          
          stage.setScene(new Scene(b1));
          stage.show();
        }
        
        private static ContextMenu createContextMenu(String prefix) {
      final ContextMenu contextMenu = new ContextMenu();
      MenuItem item1 = new MenuItem(prefix + ": About");
      item1.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
      System.out.println("About");
      }
      });
      MenuItem item2 = new MenuItem(prefix + ": Preferences");
      item2.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
      System.out.println("Preferences");
      }
      });
      contextMenu.getItems().addAll(item1, item2);
      return contextMenu;
        }
      }

            jgiles Jonathan Giles
            dgilbertjfx David Gilbert (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: