]> git.basschouten.com Git - openhab-addons.git/commitdiff
Avoids NPE if no station provided by API (#15832)
authorGaël L'hopital <gael@lhopital.org>
Fri, 3 Nov 2023 17:17:36 +0000 (18:17 +0100)
committerGitHub <noreply@github.com>
Fri, 3 Nov 2023 17:17:36 +0000 (18:17 +0100)
Signed-off-by: clinique <gael@lhopital.org>
bundles/org.openhab.binding.vigicrues/src/main/java/org/openhab/binding/vigicrues/internal/handler/VigiCruesHandler.java

index a3426f6dbbe65635f664b754b71f14e7ed8efa73..7cd2a167b6d6330203de0e6cfa09ed5a00e7fdac 100644 (file)
@@ -34,6 +34,7 @@ import org.openhab.binding.vigicrues.internal.StationConfiguration;
 import org.openhab.binding.vigicrues.internal.api.ApiHandler;
 import org.openhab.binding.vigicrues.internal.api.VigiCruesException;
 import org.openhab.binding.vigicrues.internal.dto.hubeau.HubEauResponse;
+import org.openhab.binding.vigicrues.internal.dto.hubeau.HubEauResponse.StationData;
 import org.openhab.binding.vigicrues.internal.dto.opendatasoft.OpenDatasoftResponse;
 import org.openhab.binding.vigicrues.internal.dto.vigicrues.CdStationHydro;
 import org.openhab.binding.vigicrues.internal.dto.vigicrues.InfoVigiCru;
@@ -129,17 +130,22 @@ public class VigiCruesHandler extends BaseThingHandler {
 
         try {
             HubEauResponse stationDetails = apiHandler.discoverStations(config.id);
-            stationDetails.stations.stream().findFirst().ifPresent(station -> {
-                PointType stationLocation = new PointType(
-                        String.format(Locale.US, "%f,%f", station.latitudeStation, station.longitudeStation));
-                properties.put(LOCATION, stationLocation.toString());
-                PointType serverLocation = locationProvider.getLocation();
-                if (serverLocation != null) {
-                    DecimalType distance = serverLocation.distanceFrom(stationLocation);
-                    properties.put(DISTANCE, new QuantityType<>(distance, SIUnits.METRE).toString());
-                }
-                properties.put(RIVER, station.libelleCoursEau);
-            });
+            List<StationData> stations = stationDetails.stations;
+            if (stations != null && stations.size() > 0) {
+                stationDetails.stations.stream().findFirst().ifPresent(station -> {
+                    PointType stationLocation = new PointType(
+                            String.format(Locale.US, "%f,%f", station.latitudeStation, station.longitudeStation));
+                    properties.put(LOCATION, stationLocation.toString());
+                    PointType serverLocation = locationProvider.getLocation();
+                    if (serverLocation != null) {
+                        DecimalType distance = serverLocation.distanceFrom(stationLocation);
+                        properties.put(DISTANCE, new QuantityType<>(distance, SIUnits.METRE).toString());
+                    }
+                    properties.put(RIVER, station.libelleCoursEau);
+                });
+            } else {
+                throw new VigiCruesException("No stations provided");
+            }
         } catch (VigiCruesException e) {
             logger.info("Unable to retrieve station location details {} : {}", config.id, e.getMessage());
         }