Skip to content

[java] NonThreadSafeSingleton: throws NoSuchElementException on a record that null checks its own fields #7120

Closed

Description

@Jallibad

Affects PMD Version:
7.28.0

Description:
PMD 7.28.0 throws an exception when evaluating the NonThreadSafeSingleton rule against a Java record that null checks its own fields. The issue seems to be here, a record's fields will be under ASTRecordComponent instead of ASTFieldDeclaration.

Exception Stacktrace:

[WARN] Exception applying rule NonThreadSafeSingleton on file /data/code/pmd/repro-7120/Foo.java, continuing with next rule
org.apache.commons.lang3.exception.ContextedRuntimeException: java.util.NoSuchElementException: Empty node stream
Exception Context:
        [1:Rule applied on node=!debug only! [MethodDeclaration:2:10]void baz() {
        if (field == null) {
            field.toString();
        }
    }]
---------------------------------
        at net.sourceforge.pmd.util.AssertionUtil.contexted(AssertionUtil.java:265)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:79)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.apply(RuleApplicator.java:57)
        at net.sourceforge.pmd.lang.rule.internal.RuleSets.apply(RuleSets.java:183)
        at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource(PmdRunnable.java:140)
        at net.sourceforge.pmd.lang.impl.PmdRunnable.run(PmdRunnable.java:80)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
        at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: java.util.NoSuchElementException: Empty node stream
        at net.sourceforge.pmd.lang.ast.NodeStream.firstOrThrow(NodeStream.java:782)
        at net.sourceforge.pmd.lang.java.rule.multithreading.NonThreadSafeSingletonRule.shouldConsiderField(NonThreadSafeSingletonRule.java:66)
        at net.sourceforge.pmd.lang.java.rule.multithreading.NonThreadSafeSingletonRule.isNullCheckOnField(NonThreadSafeSingletonRule.java:132)
        at net.sourceforge.pmd.lang.java.rule.multithreading.NonThreadSafeSingletonRule.visit(NonThreadSafeSingletonRule.java:79)
        at net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration.acceptVisitor(ASTMethodDeclaration.java:57)
        at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor(AbstractJavaNode.java:38)
        at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.apply(AbstractJavaRule.java:30)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:77)
        ... 9 more
[WARN] Exception occurred on node !debug only! [MethodDeclaration:2:10]void baz() {
        if (field == null) {
            field.toString();
        }
    }
Foo.java        -       ContextedRuntimeException: java.util.NoSuchElementException: Empty node stream
Exception Context:
        [1:Rule applied on node=!debug only! [MethodDeclaration:2:10]void baz() {
        if (field == null) {
            field.toString();
        }
    }]
---------------------------------

Code Sample demonstrating the issue:

record Foo(Bar field) {
    void baz() {
        if (field == null) {
            field.toString();
        }
    }
}

Steps to reproduce:
Run PMD with NonThreadSafeSingleton enabled against a Java record that checks if a field is null using == null in any method.

Running PMD through:
Gradle / CLI

Activity

  1. added
    a:bug
    on Sep 28, 2026
  2. changed the title [-][java] NPE thrown when evaluating NonThreadSafeSingleton on a record that null checks its own fields[/-] [+][java] NoSuchElementException thrown when evaluating NonThreadSafeSingleton on a record that null checks its own fields[/+] on Sep 28, 2026
  3. changed the title [-][java] NoSuchElementException thrown when evaluating NonThreadSafeSingleton on a record that null checks its own fields[/-] [+][java] NonThreadSafeSingleton: throws NoSuchElementException on a record that null checks its own fields[/+] on Oct 23, 2026
  4. added this to the 7.29.0 milestone on Oct 23, 2026
  5. added a commit that references this issue on Oct 23, 2026
    46cfb1d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions