JDK-8301989 : new javax.swing.text.DefaultCaret().setBlinkRate(N) results in NPE
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 20,20.0.1,21
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2023-02-07
  • Updated: 2025-02-22
  • Resolved: 2023-02-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 17 JDK 20 JDK 21 JDK 8
11.0.27-oracleFixed 17.0.15-oracleFixed 20 b36Fixed 21Fixed 8u451Fixed
Related Reports
Relates :  
Description
Execution of the following code:

        new DefaultCaret().setBlinkRate(100);

leads to

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "javax.swing.text.JTextComponent.isEditable()" because "this.component" is null
	at java.desktop/javax.swing.text.DefaultCaret.setBlinkRate(DefaultCaret.java:1053)
Comments
Fix request [17u] I backport this for parity with 17.0.15-oracle. Low risk, simple fix in java.desktop. Clean backport. Test passes. SAP nightly testing passed.
21-02-2025

A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk17u-dev/pull/3284 Date: 2025-02-20 13:23:31 +0000
20-02-2025

Changeset: e81f20b5 Author: Alexander Zuev <kizune@openjdk.org> Date: 2023-02-08 23:55:58 +0000 URL: https://git.openjdk.org/jdk20/commit/e81f20b50405dc02963c8bf549000c60e78152d8
08-02-2023

I am unhappy with the justification, "minimal and non-destrcutive" Minimal means small but a small change can be really high risk "non-destructive" is just an uninterpretable comment. I have zero idea what you mean. And "crash" ? you mean the VM exits ? oh, you mean an NPE .. that is NOT a crash. So start over and write a new justification with clear explanation and details and no generalisation.
08-02-2023

Fix Request Reason: While implementing fix for the bug JDK-4512626 the new code was added that slightly alters the behaviour of the default Caret implementation for Swing text based components. The fix was contained within the javax.swing.text.DefaultCaret class and employed analyzing of the editability status of the component to which the caret is assigned. In doing so the new code fails to take into account possibility that the caret can be initialized and configured separately from the component and assigned to the component later. Because of that the method can be called from a null which causes a NullPointerException. If such code exists in the client application that can cause a slew of side effects ranging from missing UI components to abnormal termination of the application. In addition this behaviour is undocumented and as such was deemed as a JCK certification failure by the JCK team. Giving all that with heavy hart must I request to amend the code on such a late stage of the development. The idea of the proposed fix is simple: we should ensure that there is a text component associated with the Caret before calling methods from it. In case when the component is not yet assigned we should safely store the provided value in a way that will preserve the Caret configuration so when caret is assigned to the component it will not loose customization of the blink rate made by the user. The nature of the fix makes code safer and the fix in question will not bring any additional instability to the amended class. Fix was tested using both automated regression tests for the components affected by the change and with manual tests that were created to simulate various scenarios of the DefaultCaret use on different text components (JTextArea, JTextField, JTextPane). Risk: Low Testing: New automated regression test test/jdk/javax/swing/text/DefaultCaret/SetCaretRateTest.java is created to prevent the potential unnoticed reappearance of the problem.
08-02-2023

Fix Request Reason: This is a fix for regression that potentially can cause client applications to crash with the new JDK. Regression was introduced by the fix for JDK-4512626. The change is minimal and non-destructive. Risk: low Testing: new automated regression test test/jdk/javax/swing/text/DefaultCaret/SetCaretRateTest.java is created to track this issue.
07-02-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk20/pull/122 Date: 2023-02-07 22:49:55 +0000
07-02-2023

While trying to implement a logic of tracking the editable state of the component we neglected the check that the component is already assigned.
07-02-2023