Skip to content

PgResultSet.getObject() throws java.lang.AssertionError: Misuse of castNonNull for null column values #1870

@dcitron

Description

@dcitron

Describe the issue
The changes introduced in PR #1814 appear to have broken PgResultSet.getObject(int, Class<T>) for PGobjects when the database column value is null and JRE assertions are enabled (-ea).

The following code in PgConnection.getObject(String, String, byte[]) appears to be the culprit (line 658 and 665):

        obj = new PGobject();
        obj.setType(type);
        obj.setValue(castNonNull(value));

The org.postgresql.util.internal.Nullness.castNonNull(T) implementation contains an assertion, and, in any case, the value is allowed to be null at that point.

Driver Version?
42.2.16

Java Version?
JDK11 (or any version)

OS Version?
Linux

PostgreSQL Version?
9.6

To Reproduce
Steps to reproduce the behaviour:

Expected behaviour
This simple test will exhibit the issue when run with java -ea ...

class Scratch {
    public static void main(String[] args) throws Exception {
        String url = "jdbc:postgresql://localhost:5432/test";

        Properties props = new Properties();
        props.setProperty("user", "test");
        props.setProperty("password", "test");
        try (Connection conn = DriverManager.getConnection(url, props)) {
            try (Statement statement = conn.createStatement()) {
                try (ResultSet rs = statement.executeQuery("select null::jsonb")) {
                    if (rs.next())
                        System.out.println("Get object: " + rs.getObject(1, PGobject.class));
                }
            }
        }
    }
}

Stacktrace:

Exception in thread "main" java.lang.AssertionError: Misuse of castNonNull: called with a null argument
	at org.postgresql.util.internal.Nullness.castNonNull(Nullness.java:22)
	at org.postgresql.jdbc.PgConnection.getObject(PgConnection.java:665)
	at org.postgresql.jdbc.PgResultSet.getObject(PgResultSet.java:3603)
	at Scratch.main(scratch_6.java:20)

A stacktrace from a more typical use-case:

Caused by: java.lang.AssertionError: Misuse of castNonNull: called with a null argument
	at org.postgresql.util.internal.Nullness.castNonNull(Nullness.java:22)
	at org.postgresql.jdbc.PgConnection.getObject(PgConnection.java:665)
	at org.postgresql.jdbc.PgResultSet.getObject(PgResultSet.java:3603)
	at com.mchange.v2.c3p0.impl.NewProxyResultSet.getObject(NewProxyResultSet.java:217)
	at org.springframework.jdbc.support.JdbcUtils.getResultSetValue(JdbcUtils.java:229)
	at org.springframework.jdbc.core.BeanPropertyRowMapper.getColumnValue(BeanPropertyRowMapper.java:378)
	at org.springframework.jdbc.core.BeanPropertyRowMapper.mapRow(BeanPropertyRowMapper.java:299)
	at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:94)
	at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:61)
	at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:679)
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:617)
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:669)
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:694)
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:748)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions