GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] CloseResourceRule: NullPointerException while analyzing · Issue #5974 · pmd/pmd

[java] CloseResourceRule: NullPointerException while analyzing #5974

New issue
Copy link
New issue
Copy link
Closed
#5979
Closed
[java] CloseResourceRule: NullPointerException while analyzing#5974
#5979
Copy link
Labels
a:bugPMD crashes or fails to analyse a file.
Milestone
7.17.0

Description

@stevenschlansker
stevenschlansker
opened on Aug 7, 2025
Issue body actions

Affects PMD Version:

7.16.0

Description:

With the following code, the CloseResourceRule throws NullPointerException.

Exception Stacktrace:

[INFO] --- pmd:3.27.0:pmd (default-cli) @ my-project ---
[INFO] PMD version: 7.16.0
[WARNING] Exception applying rule CloseResource on file /mypackage/CloseResourceCrasher.java, continuing with next rule
org.apache.commons.lang3.exception.ContextedRuntimeException: java.lang.NullPointerException: Cannot invoke "net.sourceforge.pmd.lang.java.ast.TypeNode.getTypeMirror()" because "type" is null
Exception Context:
	[1:Rule applied on node=!debug only! [CompilationUnit:1:1]package mypackage;

import java.io.Closeable;
import java.util.List;

public class CloseResourceCrasher implements (truncated)]
---------------------------------
    at net.sourceforge.pmd.util.AssertionUtil.contexted (AssertionUtil.java:251)
    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.util.concurrent.Executors$RunnableAdapter.call (Executors.java:545)
    at java.util.concurrent.FutureTask.run (FutureTask.java:328)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1095)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:619)
    at java.lang.Thread.run (Thread.java:1447)
Caused by: java.lang.NullPointerException: Cannot invoke "net.sourceforge.pmd.lang.java.ast.TypeNode.getTypeMirror()" because "type" is null
    at net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule.getResourceTypeName (CloseResourceRule.java:685)
    at net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule.addCloseResourceViolation (CloseResourceRule.java:676)
    at net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule.checkForResources (CloseResourceRule.java:183)
    at net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule.visit (CloseResourceRule.java:165)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration.acceptVisitor (ASTMethodDeclaration.java:55)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.lambda$visitNode$0 (AbstractJavaRule.java:24)
    at net.sourceforge.pmd.lang.ast.internal.StreamImpl$2.forEach (StreamImpl.java:100)
    at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.visitNode (AbstractJavaRule.java:24)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitor.visitJavaNode (JavaVisitor.java:7)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitor.visit (JavaVisitor.java:9)
    at net.sourceforge.pmd.lang.java.ast.ASTClassBody.acceptVisitor (ASTClassBody.java:26)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.lambda$visitNode$0 (AbstractJavaRule.java:24)
    at net.sourceforge.pmd.lang.ast.internal.StreamImpl$2.forEach (StreamImpl.java:100)
    at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.visitNode (AbstractJavaRule.java:24)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitor.visitJavaNode (JavaVisitor.java:7)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitor.visit (JavaVisitor.java:14)
    at net.sourceforge.pmd.lang.java.ast.ASTClassDeclaration.acceptVisitor (ASTClassDeclaration.java:39)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.lambda$visitNode$0 (AbstractJavaRule.java:24)
    at net.sourceforge.pmd.lang.ast.internal.StreamImpl$2.forEach (StreamImpl.java:100)
    at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.visitNode (AbstractJavaRule.java:24)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitor.visitJavaNode (JavaVisitor.java:7)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitor.visit (JavaVisitor.java:8)
    at net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit.acceptVisitor (ASTCompilationUnit.java:106)
    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.RuleReference.apply (RuleReference.java:413)
    at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex (RuleApplicator.java:77)
    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.util.concurrent.Executors$RunnableAdapter.call (Executors.java:545)
    at java.util.concurrent.FutureTask.run (FutureTask.java:328)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1095)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:619)
    at java.lang.Thread.run (Thread.java:1447)
[WARNING] Exception occurred on node !debug only! [CompilationUnit:1:1]package mypackage;

import java.io.Closeable;
import java.util.List;

public class CloseResourceCrasher implements (truncated)

Code Sample demonstrating the issue:

package mypackage;

import java.io.Closeable;
import java.util.List;

public class CloseResourceCrasher implements Closeable {
    List<Closeable> connections;

    @Override
    public void close() {
        final List<Closeable> openConnections;
        synchronized (this) {
            openConnections = List.copyOf(connections);
        }
        for (final var c : openConnections) {
            try {
                c.close();
            } catch (final Exception e) {
            }
        }
    }
}

Steps to reproduce:
mvn pmd:pmd

Running PMD through: Maven

Activity

  1. stevenschlansker
    added
    a:bugPMD crashes or fails to analyse a file.
    on Aug 7, 2025
  2. lukasgraef
    added 2 commits that reference this issue on Aug 8, 2025

    Fix pmd#5974: [java] NPE in CloseResourceRule

    This issue will close once commit 9b3bd7c is merged into the 'master' branch.
    9b3bd7c

    pmd#5974: Review finding: Improve testcase

    This issue will close once commit 10f053f is merged into the 'master' branch.
    10f053f
  3. adangel
    added this to the 7.17.0 milestone on Aug 16, 2025
  4. adangel
    changed the title [-][java] CloseResourceRule NullPointerException analyzing[/-] [+][java] CloseResourceRule: NullPointerException while analyzing[/+] on Aug 16, 2025
  5. adangel
    closed this as completedin 9b3bd7con Aug 16, 2025
  6. adangel
    added a commit that references this issue on Aug 16, 2025

    Fix #5974: [java] NPE in CloseResourceRule (#5979)

    This issue will close once commit b69a59d is merged into the 'main' branch.
    b69a59d
  7. UncleOwen
    marked [java] PMD 7.16.0 CloseResource rule throws exception for nested AutoClosable logic #6017 as a duplicate of this issue on Aug 27, 2025
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:bugPMD crashes or fails to analyse a file.

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.17.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.