From 84355c2dcf108dcb1f70c3ed86487b08b9d603b9 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 26 Oct 2014 02:47:37 +0300 Subject: [PATCH] Fixed crash when values is read from RestApi before fully initialized (fixes #159). --- .../syncthingandroid/test/syncthing/RestApiTest.java | 12 ++++++------ .../nutomic/syncthingandroid/syncthing/RestApi.java | 4 ++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/RestApiTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/RestApiTest.java index a774047e..f9dd0a4f 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/RestApiTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/RestApiTest.java @@ -69,12 +69,6 @@ public class RestApiTest extends AndroidTestCase { ConfigXml.getConfigFile(new MockContext(getContext())).delete(); } - @SmallTest - public void testGetVersion() { - assertNotNull(mApi.getVersion()); - assertFalse(mApi.getVersion().equals("")); - } - @SmallTest public void testGetDevices() { assertNotNull(mApi.getDevices()); @@ -151,4 +145,10 @@ public class RestApiTest extends AndroidTestCase { latch.await(1, TimeUnit.SECONDS); } + @SmallTest + public void testGetValueEarly() { + // Should never throw an exception. + mApi.getValue("Options", "ListenAddress"); + } + } diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index e7e7e95c..b72928eb 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -279,6 +279,10 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, * @return The value as a String, or null on failure. */ public String getValue(String name, String key) { + // Happens if this functions is called before class is fully initialized. + if (mConfig == null) + return null; + try { Object value = mConfig.getJSONObject(name).get(key); return (value instanceof JSONArray)