Skip to content

[java] CheckResultSet: False positive with ResultSet.next() in a while loop #5031

Open

Description

@garydgregory

Affects PMD Version: 7.1.0

Rule: CheckResultSet

Description:

From Apache Commons DbUtils:

    public <T> List<T> toBeanList(final ResultSet resultSet, final Class<? extends T> type) throws SQLException {
        final List<T> results = new ArrayList<>();
        if (!resultSet.next()) {
            return results;
        }
        final PropertyDescriptor[] props = this.propertyDescriptors(type);
        final ResultSetMetaData rsmd = resultSet.getMetaData();
        final int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);
        do {
            results.add(this.createBean(resultSet, type, props, columnToProperty));
        } while (resultSet.next()); // NOPMD False positive CheckResultSet
        return results;
    }

Expected outcome:

PMD reports a violation in BeanProcessor.java, but that's wrong. That's a false positive.

<suppressedviolation filename="C:\Users\ggregory\git\a\commons-dbutils\src\main\java\org\apache\commons\dbutils\BeanProcessor.java"
                     suppressiontype="//nopmd"
                     msg="Always check the return of one of the navigation method (next,previous,first,last) of a ResultSet."
                     usermsg=" False positive CheckResultSet

Running PMD through: Maven

Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)
Maven home: C:\java\apache-maven-3.9.6
Java version: 17.0.11, vendor: Eclipse Adoptium, runtime: C:\Program Files\Eclipse Adoptium\jdk-17.0.11.9-hotspot
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

Activity

  1. added
    a:false-positive
    on May 22, 2024
  2. changed the title [-][jaav] False positive CheckResultSet with ResultSet.next() in a while loop[/-] [+][java] False positive CheckResultSet with ResultSet.next() in a while loop[/+] on May 22, 2024
  3. changed the title [-][java] False positive CheckResultSet with ResultSet.next() in a while loop[/-] [+][java] CheckResultSet: False positive with ResultSet.next() in a while loop[/+] on Sep 12, 2024
  4. jbisotti commented on Aug 29, 2025

    @jbisotti

    I'm seeing the same this with this code...

            boolean resultSetHasNext = resultSet.next();
            while (resultSetHasNext && resultMap.size() < maxResultCap) {
                ...
                resultSetHasNext = resultSet.next();
            }
  5. UncleOwen commented on Apr 5, 2026

    @UncleOwen
    Member

    @jbisotti I tried to reproduce using PMD 7.23 and the following code:

    import java.sql.*;
    import java.util.*;
    
    class Foo {
        public void toBeanList(final ResultSet resultSet) throws SQLException {
            Map<String, String> resultMap = new HashMap<>();
            int maxResultCap = 10;
    
            boolean resultSetHasNext = resultSet.next();
            while (resultSetHasNext && resultMap.size() < maxResultCap) {
                resultSetHasNext = resultSet.next();
            }
        }
    }
    

    I get no warning.

    With the code from @garydgregory I do get a warning.

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:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions