Skip to content

Commit c47c5cb

Browse files
committed
8376169: JPopupMenu.setInvoker(null) causes NPE
1 parent 0f087a7 commit c47c5cb

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/java.desktop/share/classes/javax/swing/JPopupMenu.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -952,18 +952,20 @@ && isVisible()) {
952952
@BeanProperty(bound = false, expert = true, description
953953
= "The invoking component for the popup menu")
954954
public void setInvoker(Component invoker) {
955-
Component oldInvoker = this.invoker;
956-
this.invoker = invoker;
955+
if (invoker != null) {
956+
Component oldInvoker = this.invoker;
957+
this.invoker = invoker;
957958

958-
if ((oldInvoker != this.invoker) && (ui != null)) {
959-
ui.uninstallUI(this);
960-
if (oldInvoker != null) {
961-
oldInvoker.removePropertyChangeListener("ancestor", propListener);
959+
if ((oldInvoker != this.invoker) && (ui != null)) {
960+
ui.uninstallUI(this);
961+
if (oldInvoker != null) {
962+
oldInvoker.removePropertyChangeListener("ancestor", propListener);
963+
}
964+
invoker.addPropertyChangeListener("ancestor", propListener);
965+
ui.installUI(this);
962966
}
963-
invoker.addPropertyChangeListener("ancestor", propListener);
964-
ui.installUI(this);
967+
invalidate();
965968
}
966-
invalidate();
967969
}
968970

969971
/**

test/jdk/javax/swing/JPopupMenu/TestPopupInvoker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -108,6 +108,7 @@ public static void main(String[] args) throws Exception {
108108
}
109109
} finally {
110110
SwingUtilities.invokeAndWait(() -> {
111+
popupMenu.setInvoker(null);
111112
if (frame != null) {
112113
frame.dispose();
113114
}

0 commit comments

Comments
 (0)