[Dive4elements-commits] [PATCH] Fixed NPE problem in fetching extram from discharge curves

Wald Commits scm-commit at wald.intevation.org
Fri May 10 12:23:01 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1368181376 -7200
# Node ID abe5fe3d2d173db1ba8f74ef0ff1f8f131f9e87a
# Parent  f0550bac4468d6ff01096f8b35470aba3561e131
Fixed NPE problem in fetching extram from discharge curves.

diff -r f0550bac4468 -r abe5fe3d2d17 backend/src/main/java/org/dive4elements/river/model/Gauge.java
--- a/backend/src/main/java/org/dive4elements/river/model/Gauge.java	Fri May 10 11:41:43 2013 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/Gauge.java	Fri May 10 12:22:56 2013 +0200
@@ -12,6 +12,7 @@
 
 import java.io.Serializable;
 
+import java.util.Arrays;
 import java.util.List;
 
 import javax.persistence.Entity;
@@ -28,6 +29,8 @@
 import org.hibernate.Session;
 import org.hibernate.Query;
 
+import org.apache.log4j.Logger;
+
 import org.dive4elements.river.backend.SessionHolder;
 
 /** Database-mapped Gauge with all info about it. */
@@ -36,6 +39,8 @@
 public class Gauge
 implements   Serializable, Comparable<Gauge>
 {
+    private static final Logger log = Logger.getLogger(Gauge.class);
+
     public static final int DEFAULT_SCALE = 100;
 
     public static final int MASTER_DISCHARGE_TABLE = 0;
@@ -205,13 +210,19 @@
             "where table_id =:table");
         query.setParameter("table", dischargeTable.getId());
 
-        List     results = query.list();
-        Object[] result  = (Object[]) results.get(0);
+        List<?> results = query.list();
+        if (results.isEmpty()) {
+            log.error("No values in discharge table found.");
+            return null;
+        }
 
-        return result != null
-            ? new double[] {
-                ((BigDecimal) result[0]).doubleValue() * scale,
-                ((BigDecimal) result[1]).doubleValue() * scale}
+        Object[] result  = (Object[])results.get(0);
+
+        BigDecimal a = (BigDecimal)result[0];
+        BigDecimal b = (BigDecimal)result[1];
+
+        return a != null && b != null
+            ? new double [] { a.doubleValue()*scale, b.doubleValue()*scale }
             : null;
     }
 


More information about the Dive4elements-commits mailing list