Mercurial > dive4elements > river
changeset 5969:abe5fe3d2d17
Fixed NPE problem in fetching extram from discharge curves.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Fri, 10 May 2013 12:22:56 +0200 |
parents | f0550bac4468 |
children | 9b1eb9a52224 |
files | backend/src/main/java/org/dive4elements/river/model/Gauge.java |
diffstat | 1 files changed, 17 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- 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; }