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

"NullPointerException: peer" when setting focused text field read-only

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Windows 11

      java -version:
        openjdk version "21.0.8" 2025-07-15 LTS
        OpenJDK Runtime Environment Temurin-21.0.8+9 (build 21.0.8+9-LTS)
        OpenJDK 64-Bit Server VM Temurin-21.0.8+9 (build 21.0.8+9-LTS, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      This bug happens on Windows if you focus a text field, switch to another application, and then directly click on a component whose mousePressed listener calls setEditable(false) on the text field.

      The NPE happens when calling WInputMethod.openCandidateWindow in the Runnable of WInputMethod.inquireCandidatePosition because awtFocussedComponentPeer is null. The null value comes from JTextComponent.setEditable -> Component.enableInputMethods -> InputContext.removeNotify - WInputMethod.removeNotify.

      If this Java application is already focused, WInputMethod.inquireCandidatePosition is not called, so the null peer is not a problem.
      If setEditable is called from mouseClicked/actionPerformed, then InputContext.removeNotify doesn't call WInputMethod.removeNotify because currentClientComponent has already been set to the button instance.

      I hope this little analysis helps.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Execute the provided code.
      2. Focus the text field.
      3. Focus some other application, e.g. a browser.
      4. Directly click on the button "Set read-only" while the other application is still focused.
      5. See the exception printed to the console.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The text field should be set read-only without any exceptions.
      ACTUAL -
      The text field is set read-only, but also the following exception is thrown:

      Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: peer
      at java.desktop/sun.awt.windows.WInputMethod.openCandidateWindow(Native Method)
      at java.desktop/sun.awt.windows.WInputMethod$1.run(WInputMethod.java:627)
      at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
      at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
      at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
      at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
      at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
      at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
      at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:98)
      at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747)
      at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
      at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
      at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
      at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744)
      at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
      at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
      at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
      at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
      at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
      at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

      ---------- BEGIN SOURCE ----------
      package swing;

      import java.awt.BorderLayout;
      import java.awt.event.MouseAdapter;
      import java.awt.event.MouseEvent;

      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JTextField;
      import javax.swing.WindowConstants;


      public class PeerNPE {

      public static void main(String[] args) {
      var frame = new JFrame();
      var textField = new JTextField();
      var button = new JButton("Set read-only");
      button.addMouseListener(
      new MouseAdapter() {
      @Override
      public void mousePressed(MouseEvent e) {
      textField.setEditable(false);
      }
      }
      );
      frame.getContentPane().setLayout(new BorderLayout());
      frame.getContentPane().add(textField, BorderLayout.CENTER);
      frame.getContentPane().add(button, BorderLayout.SOUTH);
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.pack();
      frame.setVisible(true);
      }
      }

      ---------- END SOURCE ----------

        1. PeerNPE.java
          1 kB
          Patricia Tavares

            honkar Harshitha Onkar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: