RFR: 8376492: NullPointer in ContextMenu sub-menu when graphic and style classes are changed while the menu is open
eduardsdv
duke at openjdk.org
Tue Feb 17 08:23:41 UTC 2026
On Fri, 13 Feb 2026 11:36:17 GMT, Dmitry Markov <dmarkov at openjdk.org> wrote:
> A NullPointerException occurs when a ContextMenu submenu is opened for the second time and the graphic and style classes are modified while the menu is open.
>
> Fix: Added a null check.
modules/javafx.controls/src/main/java/com/sun/javafx/scene/control/ContextMenuContent.java line 1299:
> 1297: };
> 1298: item.getStyleClass().addListener(itemStyleClassListener);
> 1299: item.getProperties().put(ITEM_STYLE_CLASS_LISTENER, itemStyleClassListener);
I think the reason for the issue is, that the listener ``itemStyleClassListener`` is created and added each time the ``createChildren()`` method is called. This happens once in the constructor and each time the ``graphicProperty()`` is changed (see the lines 1188-1191).
So, after some changes of ``graphicProperty()`` we have a number of listeners added to the ``item.getStyleClass()``, but only last one will be removed when ``dispose()`` is called.
The ``dispose()`` method also nulls the ``label``. Therefore, if the ``item.getStyleClass()`` is changed afterwards, the listeners that have not been removed will attempt to update the **null**-label.
Therefore, I think, we not only have a NullPointerException but also a memory leak.
What about removing the old listener when a new one is created and added to the ``item.getStyleClass()`` method, as is done in the ``dispose()`` method?
ListChangeListener<String> previousItemStyleClassListener = (ListChangeListener<String>)item.getProperties().put(ITEM_STYLE_CLASS_LISTENER, itemStyleClassListener);
if (previousItemStyleClassListener!= null) {
item.getStyleClass().removeListener(previousItemStyleClassListener);
}
-------------
PR Review Comment: https://git.openjdk.org/jfx/pull/2075#discussion_r2815568733
More information about the openjfx-dev
mailing list