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

JTextField: NPE after setting Editable property in Document Listener

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_162"
      Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
      Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Verze 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      I have a swing application, with GUI.
      There are two JTextFields that depend on each other - only one is editable at the time. When I want to switch between the text fields, I type minus "-" into it. Then, there is a Document listener that consumes the minus, nulls the content, changes the editable properties and moves the focus.

      The method works in most of the cases, when I work continuously with the window, but when I switch to another window and then back to my app and try the field switch, it fails.

      The problematic part is setting the Editable property. When I set the value to false, an event is added into the event queue that fails with NPE.

      REGRESSION. Last worked in version 8u161

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.8.0_161"
      Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
      Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the application JumpBetweenTextFields
      Focus one of the input fields
      Click somewhere else to another application, so that the frame is not selected anymore
      Click to the JumpBetweenTextFields again, but not into the text fields, text field keeps its focus, so I can start typing right away.

      When the first thing I type, after the window regained its focus, is minus to switch the fields, the error is thrown.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      no error + switching between the text fields with focus transfer
      ACTUAL -
      Error, but also switching between the text fields with focus transfer

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: peer
      at sun.awt.windows.WInputMethod.openCandidateWindow(Native Method)
      at sun.awt.windows.WInputMethod.access$400(WInputMethod.java:45)
      at sun.awt.windows.WInputMethod$1.run(WInputMethod.java:599)
      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
      at java.awt.EventQueue.access$500(EventQueue.java:97)
      at java.awt.EventQueue$3.run(EventQueue.java:709)
      at java.awt.EventQueue$3.run(EventQueue.java:703)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
      at java.awt.EventQueue$4.run(EventQueue.java:733)
      at java.awt.EventQueue$4.run(EventQueue.java:731)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Dimension;
      import java.awt.FlowLayout;

      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JTextField;
      import javax.swing.SwingUtilities;
      import javax.swing.event.DocumentEvent;
      import javax.swing.event.DocumentEvent.EventType;
      import javax.swing.text.BadLocationException;

      import cz.tipsport.common.gui.classes.DocumentListenerAdapter;

      public class JumpBetweenTextFields {

      private static void createAndShowGUI() {
      JFrame frame = new JFrame("changing text fields");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setPreferredSize(new Dimension(300, 300));

      JPanel p = new JPanel(new FlowLayout());

      final JTextField A = new JTextField();
      A.setPreferredSize(new Dimension(60, 30));
      final JTextField B = new JTextField();
      B.setPreferredSize(new Dimension(60, 30));
      installMinusLogic(A, B);
      installMinusLogic(B, A);
      p.add(A);
      p.add(B);
      B.setEditable(false);

      frame.getContentPane().add(p);
      frame.pack();
      frame.setVisible(true);
      }

      public static void main(String[] args) {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
      @Override public void run() {
      createAndShowGUI();
      }
      });
      }

          private static void installMinusLogic(final JTextField t1, final JTextField t2) {
              t1.getDocument().addDocumentListener(new DocumentListenerAdapter() {
                  @Override
                  protected void processUpdate(final DocumentEvent e) {
                      if ((EventType.INSERT.equals(e.getType()) || EventType.CHANGE.equals(e.getType())) && e.getLength() == 1) {
                          try {
                              if ("-".equals(e.getDocument().getText(e.getOffset(), 1))) {
                                  SwingUtilities.invokeLater(new Runnable() {
                                      @Override
                                      public void run() {
                                          t1.setText("");
                                          t1.setEditable(false);
                                          t2.setEditable(true);
                                          t2.requestFocus();
                                      }
                                  });
                              }
                          } catch (final BadLocationException blex) {
                              assert (false);
                          }
                      }
                  }
              });
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      set the editable later in different event listener, in this case for example

      t2.addFocusListener(new FocusAdapter() {
                      @Override
                      public void focusGained(FocusEvent e) {
                          t1.setEditable(false);
                      }
                  });

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                pardesha Pardeep Sharma
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: