Bug #92089 PreparedStatement.setTimestamp NullPointerException
Submitted: 20 Aug 2018 15:13 Modified: 6 Jul 2019 0:44
Reporter: John Williamson Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.1.47 OS:Any (java mac osx)
Assigned to: CPU Architecture:Any

[20 Aug 2018 15:13] John Williamson
Description:
Recent change to PreparedStatement.setTimestamp throws NPE if getParameterMetaData() is called before the statement is executed.

This happens in 5.1.47.  Rolling back to 5.1.46 works fine.

Exception in thread "main" java.lang.NullPointerException
	at com.mysql.jdbc.PreparedStatement.setTimestamp(PreparedStatement.java:4241)
	at MySQL5147NPEBug.main(MySQL5147NPEBug.java:19)

How to repeat:

    public static void main(String[] args) throws Exception {

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/SomeDB?user=SomeUser&password=SomePassword&characterEncoding=UTF-8");

        PreparedStatement stmt = conn.prepareStatement(
                "insert into MySQL5147NPEBug (groupId, messagingTime, eventTime, expireTime) values (?,?,?,?)"
        );

        ParameterMetaData data = stmt.getParameterMetaData();

        stmt.setString(1, "my group id");
        stmt.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
        stmt.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
        stmt.setTimestamp(4, new Timestamp(System.currentTimeMillis()));

        stmt.executeUpdate();

    }

Suggested fix:
this.parameterMetadata.metadata.fields is null.  Add additional checks to line 4241 before accessing fields or length.
[20 Aug 2018 15:14] John Williamson
-- mysql 5.7.22

CREATE TABLE `MySQL5147NPEBug` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `groupId` varchar(40) NOT NULL,
  `messagingTime` datetime(6) DEFAULT NULL,
  `eventTime` datetime(6) DEFAULT NULL,
  `createTime` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  `expireTime` datetime(6) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
[20 Aug 2018 15:17] John Williamson
Java class to reproduce

Attachment: MySQL5147NPEBug.java (application/octet-stream, text), 878 bytes.

[21 Aug 2018 5:34] MySQL Verification Team
Hello John,

Thank you for the report!

regards,
Umesh
[25 Aug 2018 10:23] MySQL Verification Team
Bug #92190 marked as duplicate of this one
[8 Apr 2019 22:27] Kevin McLaughlin
We also experience this under heavy load on 5.1.47.  Rolling back to 5.1.46 and the error goes away.

java.lang.NullPointerException: null
        at com.mysql.jdbc.PreparedStatement.setTimestamp(PreparedStatement.java:4241)
        at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:3591)
        at com.mysql.jdbc.JDBC42PreparedStatement.setObject(JDBC42PreparedStatement.java:68)
        at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setObject(HikariProxyPreparedStatement.java)
        at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:411)
        at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:232)
        at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:147)
        at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.setValues(PreparedStatementCreatorFactory.java:286)
        at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:244)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:615)
        at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:862)
        at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:883)
        at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:320)
        at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:325)
[30 May 2019 17:18] Luís Octávio luisz
We have this error and happens in the 5.1.47 and 5.1.46. With Hibernate 4.
Now we are trying to update to 8.0.16+
[6 Jul 2019 0:44] Daniel So
Posted by developer:
 
Added the following entry to the Connector/J 5.1.48 changelog:

"PreparedStatement.setTimestamp threw a NullPointerException if getParameterMetaData() was called before the statement was executed. This fix adds the missing null checks to getParameterMetaData() to avoid the exception."