| Bug #104559 | ResultSet.getObject(i, java.util.Date.class) throws NPE when the value is null | ||
|---|---|---|---|
| Submitted: | 6 Aug 2021 14:40 | Modified: | 12 Oct 2021 16:56 | 
| Reporter: | ys y | Email Updates: | |
| Status: | Closed | Impact on me: | |
| Category: | Connector / J | Severity: | S3 (Non-critical) | 
| Version: | 8.0.23+ | OS: | Any | 
| Assigned to: | Alexander Soklakov | CPU Architecture: | Any | 
   [16 Aug 2021 9:15]
   Alexander Soklakov        
  Hi,
Thanks a lot!
Verified with the following test case:
    @Test
    public void testBug104559() throws Exception {
        String tableName = "testBug104559";
        createTable(tableName, "(`dt` DATE DEFAULT NULL)");
        this.stmt.executeUpdate("INSERT INTO " + tableName + " VALUES (null)");
        Properties props = new Properties();
        Connection con = getConnectionWithProps(props);
        Statement st = con.createStatement();
        this.rs = st.executeQuery("SELECT * FROM " + tableName);
        while (this.rs.next()) {
            System.out.println(this.rs.getObject("dt", java.util.Date.class)); // NPE !!!
        }
    }
Stack trace:
java.lang.NullPointerException
	at com.mysql.cj.jdbc.result.ResultSetImpl.getObject(ResultSetImpl.java:1346)
	at com.mysql.cj.jdbc.result.ResultSetImpl.getObject(ResultSetImpl.java:1427)
	at testsuite.simple.MetadataTest.testBug104559(MetadataTest.java:1869)
 
   [12 Oct 2021 16:56]
   Daniel So        
  Posted by developer: Added the following entry to the Connector/J 8.0.27 changelog: "When ResultSet.getObject(columnIndex, java.util.Date.class) was expected to return null, it caused Connector/J to throw a NullPointerException instead."


Description: when the value of ResultSet.getObject(columnIndex, java.util.Date.class) is null, it will prduce NPE. How to repeat: ResultSetImpl#getObject(int columnIndex, Class<T> type): else if (type.equals(java.util.Date.class)) { return (T) java.util.Date.from(getTimestamp(columnIndex).toInstant()); } getTimestamp(columnIndex) may produce NPE. Suggested fix: ResultSetImpl#getObject(int columnIndex, Class<T> type): else if (type.equals(java.util.Date.class)) { Timestamp ts = getTimestamp(columnIndex); if(ts == null){ return null; } return (T) java.util.Date.from(ts.toInstant()); }