From 1606eb6637558e3462bff6d87c0ad6bf43c85c6e Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Fri, 23 Jun 2023 17:59:23 +0800 Subject: [PATCH 1/2] fix(jdbc): ResultSet#getObject could throw a NPE Closes: #915 --- src/main/java/org/sqlite/jdbc4/JDBC4ResultSet.java | 12 +++++++++--- src/test/java/org/sqlite/ResultSetTest.java | 6 ++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/sqlite/jdbc4/JDBC4ResultSet.java b/src/main/java/org/sqlite/jdbc4/JDBC4ResultSet.java index 286917310..dd19d0a37 100644 --- a/src/main/java/org/sqlite/jdbc4/JDBC4ResultSet.java +++ b/src/main/java/org/sqlite/jdbc4/JDBC4ResultSet.java @@ -324,7 +324,9 @@ public T getObject(int columnIndex, Class type) throws SQLException { if (type == Timestamp.class) return type.cast(getTimestamp(columnIndex)); if (type == LocalDate.class) { try { - return type.cast(getDate(columnIndex).toLocalDate()); + Date date = getDate(columnIndex); + if (date != null) return type.cast(date.toLocalDate()); + else return null; } catch (SQLException sqlException) { // If the FastDateParser failed, try parse it with LocalDate. // It's a workaround for a value like '2022-12-1' (i.e no time presents). @@ -333,7 +335,9 @@ public T getObject(int columnIndex, Class type) throws SQLException { } if (type == LocalTime.class) { try { - return type.cast(getTime(columnIndex).toLocalTime()); + Time time = getTime(columnIndex); + if (time != null) return type.cast(time.toLocalTime()); + else return null; } catch (SQLException sqlException) { // If the FastDateParser failed, try parse it with LocalTime. // It's a workaround for a value like '11:22:22' (i.e no date presents). @@ -341,7 +345,9 @@ public T getObject(int columnIndex, Class type) throws SQLException { } } if (type == LocalDateTime.class) { - return type.cast(getTimestamp(columnIndex).toLocalDateTime()); + Timestamp timestamp = getTimestamp(columnIndex); + if (timestamp != null) return type.cast(timestamp.toLocalDateTime()); + else return null; } int columnType = safeGetColumnType(markCol(columnIndex)); diff --git a/src/test/java/org/sqlite/ResultSetTest.java b/src/test/java/org/sqlite/ResultSetTest.java index 1a604c850..d00590c91 100644 --- a/src/test/java/org/sqlite/ResultSetTest.java +++ b/src/test/java/org/sqlite/ResultSetTest.java @@ -335,6 +335,7 @@ void testJdk8AddedDateTimeObjects() throws SQLException { stat.executeUpdate("insert into datetime_test values ('2021-11-09 11:20:58')"); stat.executeUpdate("insert into datetime_test values ('2021-11-09')"); stat.executeUpdate("insert into datetime_test values ('11:20:58')"); + stat.executeUpdate("insert into datetime_test values (NULL)"); ResultSet rs = stat.executeQuery("select * from datetime_test"); @@ -349,6 +350,11 @@ void testJdk8AddedDateTimeObjects() throws SQLException { rs.next(); assertThat(rs.getObject(1, LocalTime.class)).isEqualTo(LocalTime.of(11, 20, 58)); + + rs.next(); + assertThat(rs.getObject(1, LocalDate.class)).isNull(); + assertThat(rs.getObject(1, LocalTime.class)).isNull(); + assertThat(rs.getObject(1, LocalDateTime.class)).isNull(); } @Test From e1d8568b4aba0d77f3b970f1bae44234a7023cdc Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 23 Jun 2023 10:02:53 +0000 Subject: [PATCH 2/2] build(deps): bump versions-maven-plugin from 2.15.0 to 2.16.0 Bumps [versions-maven-plugin](https://github.com/mojohaus/versions) from 2.15.0 to 2.16.0. - [Release notes](https://github.com/mojohaus/versions/releases) - [Changelog](https://github.com/mojohaus/versions/blob/master/ReleaseNotes.md) - [Commits](https://github.com/mojohaus/versions/compare/2.15.0...2.16.0) --- updated-dependencies: - dependency-name: org.codehaus.mojo:versions-maven-plugin dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 805b88114..81e86dd27 100644 --- a/pom.xml +++ b/pom.xml @@ -207,7 +207,7 @@ org.codehaus.mojo versions-maven-plugin - 2.15.0 + 2.16.0