Skip to content

Commit 2529e2f

Browse files
committed
8376169: JPopupMenu.setInvoker(null) causes NPE
Reviewed-by: aivanov, azvegint, prr, kizune
1 parent 09ed8e6 commit 2529e2f

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

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

Lines changed: 4 additions & 2 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
@@ -960,7 +960,9 @@ public void setInvoker(Component invoker) {
960960
if (oldInvoker != null) {
961961
oldInvoker.removePropertyChangeListener("ancestor", propListener);
962962
}
963-
invoker.addPropertyChangeListener("ancestor", propListener);
963+
if (invoker != null) {
964+
invoker.addPropertyChangeListener("ancestor", propListener);
965+
}
964966
ui.installUI(this);
965967
}
966968
invalidate();

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

Lines changed: 10 additions & 2 deletions
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
@@ -23,13 +23,14 @@
2323

2424
/*
2525
* @test
26-
* @bug 4938801
26+
* @bug 4938801 8376169
2727
* @key headful
2828
* @summary Verifies popup is removed when the component is removed
2929
* @run main TestPopupInvoker
3030
*/
3131

3232
import java.awt.BorderLayout;
33+
import java.awt.Component;
3334
import java.awt.Container;
3435
import java.awt.Robot;
3536
import java.util.concurrent.CountDownLatch;
@@ -48,6 +49,7 @@ public class TestPopupInvoker {
4849
static JFrame frame;
4950
static JLabel label;
5051
static Container pane;
52+
static volatile Component invoker;
5153

5254
private static final CountDownLatch popupShown = new CountDownLatch(1);
5355
private static final CountDownLatch popupHidden = new CountDownLatch(1);
@@ -73,6 +75,7 @@ public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
7375
@Override
7476
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
7577
popupHidden.countDown();
78+
popupMenu.setInvoker(null);
7679
}
7780

7881
@Override
@@ -106,6 +109,11 @@ public static void main(String[] args) throws Exception {
106109
if (!popupHidden.await(1, SECONDS)) {
107110
throw new RuntimeException("Popup is visible after component is removed");
108111
}
112+
113+
SwingUtilities.invokeAndWait(() -> invoker = popupMenu.getInvoker());
114+
if (invoker != null) {
115+
throw new RuntimeException("Invoker is not null");
116+
}
109117
} finally {
110118
SwingUtilities.invokeAndWait(() -> {
111119
if (frame != null) {

0 commit comments

Comments
 (0)