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

NullPointerException in MenuButton

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Cannot Reproduce
    • Affects Version/s: fx2.1
    • Fix Version/s: 7u6
    • Component/s: javafx
    • Labels:

      Description

      I get a NPE, when I try to set the graphic of a MenuButton in the ActionEvent of one of the MenuItems.

      java.lang.NullPointerException
      at com.sun.javafx.scene.control.skin.LabeledImpl$1.invalidated(Unknown Source)
      at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(Unknown Source)
      at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown Source)
      at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(Unknown Source)
      at javafx.beans.property.ObjectPropertyBase.markInvalid(Unknown Source)
      at javafx.beans.property.ObjectPropertyBase.set(Unknown Source)
      at javafx.beans.property.ObjectProperty.setValue(Unknown Source)
      at javafx.scene.control.Labeled.setGraphic(Unknown Source)


      Run the code, click the MenuButton, and then the MenuItem.

      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Label;
      import javafx.scene.control.MenuButton;
      import javafx.scene.control.MenuItem;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class TestApp3 extends Application {


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

          public void start(final Stage stage) throws Exception {


              VBox root = new VBox();

              final MenuButton button = new MenuButton("Click me");

              MenuItem item = new MenuItem("Click me");
              item.setOnAction(new EventHandler<ActionEvent>() {
                  @Override
                  public void handle(ActionEvent actionEvent) {
                      button.setGraphic(new Label("Test")); // THIS CAUSES NPE !
                  }
              });

              button.getItems().add(item);

              root.getChildren().add(button);

              Scene scene = new Scene(root);
              stage.setScene(scene);
              stage.show();
          }
      }

        Attachments

          Activity

            People

            • Assignee:
              leifs Leif Samuelsson (Inactive)
              Reporter:
              cschudtjfx Christian Schudt (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported: