From 9439e5c2e149403df50c73d7ac9eaa4121a26dcd Mon Sep 17 00:00:00 2001 From: szymon Date: Tue, 5 May 2020 23:19:15 +0200 Subject: [PATCH] Add: default path, Fix: nullpointer exception --- src/main/java/com/janas/PiRadio/API/RadioAPI.java | 5 ++++- src/main/java/com/janas/PiRadio/Radio/RadioStations.java | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/janas/PiRadio/API/RadioAPI.java b/src/main/java/com/janas/PiRadio/API/RadioAPI.java index b61a541..632d54b 100644 --- a/src/main/java/com/janas/PiRadio/API/RadioAPI.java +++ b/src/main/java/com/janas/PiRadio/API/RadioAPI.java @@ -20,7 +20,10 @@ public String validate(){ @GetMapping("/on/{name}") public void turnOnRadio(@PathVariable("name") String name ){ try { - Radio.init(radioStations.getStation(name)); + String path = radioStations.getStation(name); + if (path.length() > 0){ + Radio.init(path); + } } catch (Exception e){ System.out.println("Exception: " + e.toString()); } diff --git a/src/main/java/com/janas/PiRadio/Radio/RadioStations.java b/src/main/java/com/janas/PiRadio/Radio/RadioStations.java index d9024e3..2759773 100644 --- a/src/main/java/com/janas/PiRadio/Radio/RadioStations.java +++ b/src/main/java/com/janas/PiRadio/Radio/RadioStations.java @@ -13,14 +13,19 @@ public class RadioStations { private Map stations = new HashMap<>(); - private File file = new File("radio-stations.txt"); + private File file; public RadioStations(){ + String path = "/home/" + System.getProperty("user.name") + "/radio-stations.txt"; + file = new File(path); load(); } public String getStation(String name){ - return stations.get(name); + if (stations.containsKey(name)){ + return stations.get(name); + } + return ""; } public void putStation(String name, String url){