From 23973500069e088c1f14896f66984e1ee5356072 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Mon, 3 Feb 2020 10:24:35 -0800 Subject: [PATCH 1/2] fix nullpointer issue --- .../sqlserver/jdbc/SQLServerResultSet.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java index 5ea0469d8..58e4930d6 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java @@ -2394,12 +2394,16 @@ public T getObject(int columnIndex, Class type) throws SQLException { } else if (type == java.time.LocalDateTime.class || type == java.time.LocalDate.class || type == java.time.LocalTime.class) { java.time.LocalDateTime ldt = getLocalDateTime(columnIndex); - if (type == java.time.LocalDateTime.class) { - returnValue = ldt; - } else if (type == java.time.LocalDate.class) { - returnValue = ldt.toLocalDate(); + if (null == ldt) { + returnValue = null; } else { - returnValue = ldt.toLocalTime(); + if (type == java.time.LocalDateTime.class) { + returnValue = ldt; + } else if (type == java.time.LocalDate.class) { + returnValue = ldt.toLocalDate(); + } else { + returnValue = ldt.toLocalTime(); + } } } else if (type == java.time.OffsetDateTime.class) { microsoft.sql.DateTimeOffset dateTimeOffset = getDateTimeOffset(columnIndex); From adfa43e2182480036524901c425c3850916a7f26 Mon Sep 17 00:00:00 2001 From: Peter Bae Date: Mon, 3 Feb 2020 10:48:07 -0800 Subject: [PATCH 2/2] add tests --- .../jdbc/datatypes/DataTypesTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java index 5282debe9..634b7376b 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/DataTypesTest.java @@ -19,7 +19,9 @@ import java.text.DateFormatSymbols; import java.time.Duration; import java.time.Instant; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.time.ZoneId; import java.time.temporal.ChronoUnit; import java.time.zone.ZoneOffsetTransition; @@ -1821,6 +1823,28 @@ public void testGetLocalDateTimePriorGregorian() throws Exception { } } } + + @Test + public void testNullValuesWithGetObject() throws Exception { + String ldtTable = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("ldtTable")); + try (Connection conn = getConnection(); Statement st = conn.createStatement();) { + TestUtils.dropTableIfExists(ldtTable, st); + st.execute("CREATE TABLE " + ldtTable + " (c1 datetime2)"); + st.execute("INSERT INTO " + ldtTable + " VALUES (NULL)"); + + try (ResultSet rs = st.executeQuery("SELECT c1 FROM " + ldtTable);) { + rs.next(); + LocalDateTime ldtActual = rs.getObject(1, LocalDateTime.class); + assertEquals(ldtActual, null); + LocalTime ltActual = rs.getObject(1, LocalTime.class); + assertEquals(ltActual, null); + LocalDate ldActual = rs.getObject(1, LocalDate.class); + assertEquals(ldActual, null); + } finally { + TestUtils.dropTableIfExists(ldtTable, st); + } + } + } /** * Test example from https://github.com/microsoft/mssql-jdbc/issues/1143