codecentric / NSMenuFX

Other
128 stars 27 forks source link

NPE when new stage calls show() #21

Closed mperrone closed 7 years ago

mperrone commented 7 years ago

I'm getting the following stack trace when a new stage calls show():

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at de.codecentric.centerdevice.util.MenuBarUtils.setMenuBar(MenuBarUtils.java:29)
    at de.codecentric.centerdevice.listener.MenuBarSyncListener.lambda$onChanged$0(MenuBarSyncListener.java:37)
    at java.lang.Iterable.forEach(Iterable.java:75)
    at de.codecentric.centerdevice.listener.MenuBarSyncListener.onChanged(MenuBarSyncListener.java:37)
    at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:164)
    at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
    at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
    at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
    at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
    at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
    at javafx.collections.ModifiableObservableListBase.add(ModifiableObservableListBase.java:155)
    at java.util.AbstractList.add(AbstractList.java:108)
    at javafx.stage.Stage.impl_visibleChanging(Stage.java:1164)
    at javafx.stage.Window$9.invalidated(Window.java:824)
    at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)
    at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
    at javafx.stage.Window.setShowing(Window.java:922)
    at javafx.stage.Window.show(Window.java:937)
    at javafx.stage.Stage.show(Stage.java:259)
    at com.datastax.support.GraphicalClient.displayTicket(GraphicalClient.java:667)
    at com.datastax.support.GraphicalClient.displayTicket(GraphicalClient.java:635)
    at com.datastax.support.GraphicalClient.lambda$openMyTickets$16(GraphicalClient.java:630)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at com.datastax.support.GraphicalClient.openMyTickets(GraphicalClient.java:630)
    at com.datastax.support.GraphicalClient.start(GraphicalClient.java:465)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

I'm using JDK 1.8.0_121 on MacOS (latest)

I'm running the program from the IDE (Idea), same behavior if I build a native package.

The window menu never showed up, so I commented it out to see if the NPE still occurs, and it does. The about menu item does work properly.

Am I doing something wrong?

The following method is called from Start():

 private void setMacMenuBar() {
        final String os = System.getProperty("os.name");
        if (os != null && os.startsWith("Mac")) {
            MenuToolkit tk = MenuToolkit.toolkit();
            Menu defaultApplicationMenu = tk.createDefaultApplicationMenu("Zendesk Downloader Tool");
            tk.setApplicationMenu(defaultApplicationMenu);
            MenuBar bar = new MenuBar();
//            Menu windowMenu = new Menu("Window");
//            windowMenu.getItems().addAll(tk.createMinimizeMenuItem(), tk.createZoomMenuItem(), tk.createCycleWindowsItem(),
//                    new SeparatorMenuItem(), tk.createBringAllToFrontItem());
            bar.getMenus().addAll(defaultApplicationMenu);
//            bar.getMenus().addAll(defaultApplicationMenu, windowMenu);

         tk.setGlobalMenuBar(bar);
  }
mperrone commented 7 years ago

I've investigated enough within the debugger to conclude that I've found a bug: That it's not trivial to fix in your library (learning curve), and it's nothing to do with my code. If I don't see any interest in helping resolve the issue in the next week or so then I'll drop this library and roll my own mac specific menu code. Thanks.

0x4a616e commented 7 years ago

Hey! The problem seems to be that you cannot have a menu bar without a scene. Have you tried to set a scene first? The behaviour you described is certainly not nice, so there should probably be some more meaningful exception.

mperrone commented 7 years ago

Thanks Jan,

Agreed, there should be a check for null and a specific exception. Interestingly, scene had been set but that stage closed (login stage scene). Moving menu set up code to post next call to stage.setScene() does prevent the NPE, Please use this issue for that specific effort.

0x4a616e commented 7 years ago

might be related to #16

0x4a616e commented 7 years ago

Added a null check if no stage is set