From ef46e126b4ae22c2fcea316ff38a8fd1d4f13a19 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Tue, 4 Jan 2022 10:37:04 +0100 Subject: [PATCH] [jdbc] Fixed NPE when SQL query failed (#11954) Signed-off-by: Christoph Weitkamp --- .../java/org/openhab/persistence/jdbc/db/JdbcBaseDAO.java | 8 ++++++-- .../org/openhab/persistence/jdbc/db/JdbcDerbyDAO.java | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcBaseDAO.java b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcBaseDAO.java index 3025eef1b..b778ad48d 100644 --- a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcBaseDAO.java +++ b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcBaseDAO.java @@ -342,10 +342,14 @@ public class JdbcBaseDAO { String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name, timeZone); logger.debug("JDBC::doGetHistItemFilterQuery sql={}", sql); List m = Yank.queryObjectArrays(sql, null); + if (m == null) { + logger.debug("JDBC::doGetHistItemFilterQuery Query failed. Returning an empty list."); + return List.of(); + } // we already retrieve the unit here once as it is a very costly operation String itemName = item.getName(); Unit> unit = item instanceof NumberItem ? ((NumberItem) item).getUnit() : null; - return m.stream().map(o -> new JdbcHistoricItem(itemName, getState(item, unit, o[1]), objectAsDate(o[0]))) + return m.stream().map(o -> new JdbcHistoricItem(itemName, objectAsState(item, unit, o[1]), objectAsDate(o[0]))) .collect(Collectors. toList()); } @@ -489,7 +493,7 @@ public class JdbcBaseDAO { /***************** * H E L P E R S * *****************/ - protected State getState(Item item, @Nullable Unit> unit, Object v) { + protected State objectAsState(Item item, @Nullable Unit> unit, Object v) { logger.debug( "JDBC::ItemResultHandler::handleResult getState value = '{}', unit = '{}', getClass = '{}', clazz = '{}'", v, unit, v.getClass(), v.getClass().getSimpleName()); diff --git a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcDerbyDAO.java b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcDerbyDAO.java index c2e07d2be..ebfaa1c13 100644 --- a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcDerbyDAO.java +++ b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcDerbyDAO.java @@ -169,7 +169,7 @@ public class JdbcDerbyDAO extends JdbcBaseDAO { Unit> unit = item instanceof NumberItem ? ((NumberItem) item).getUnit() : null; return m.stream().map(o -> { logger.debug("JDBC::doGetHistItemFilterQuery 0='{}' 1='{}'", o[0], o[1]); - return new JdbcHistoricItem(itemName, getState(item, unit, o[1]), objectAsDate(o[0])); + return new JdbcHistoricItem(itemName, objectAsState(item, unit, o[1]), objectAsDate(o[0])); }).collect(Collectors. toList()); }