Repository: relayrides/pushy (java)

Issue:
NPE in ApnsPayloadBuilder.setLocalizedAlertMessage
Javadoc for ApnsPayloadBuilder.setLocalizedAlertMessage says:

> @param alertArguments arguments to populate placeholders in the localized alert string; may be {@code null}

But method will fail with NPE if null is passed as alertArgument in this row
`this.localizedAlertArguments = alertArguments.length > 0 ? alertArguments : null;`

Similar NPE will happen in other methods receiving vararg argument

Pull request:
Check for null before checking argument array length (fixes #428)
Oops. This should fix the embarrassing `NullPointerException` reported in #428.

Patch:
diff --git a/pushy/src/main/java/com/relayrides/pushy/apns/util/ApnsPayloadBuilder.java b/pushy/src/main/java/com/relayrides/pushy/apns/util/ApnsPayloadBuilder.java
index f15e95c8..fc45058e 100644
--- a/pushy/src/main/java/com/relayrides/pushy/apns/util/ApnsPayloadBuilder.java
+++ b/pushy/src/main/java/com/relayrides/pushy/apns/util/ApnsPayloadBuilder.java
@@ -161,7 +161,7 @@ public class ApnsPayloadBuilder {
      */
     public ApnsPayloadBuilder setLocalizedAlertMessage(final String localizedAlertKey, final String... alertArguments) {
         this.localizedAlertKey = localizedAlertKey;
-        this.localizedAlertArguments = alertArguments.length > 0 ? alertArguments : null;
+        this.localizedAlertArguments = (alertArguments != null && alertArguments.length > 0) ? alertArguments : null;
 
         this.alertBody = null;
 
@@ -206,7 +206,7 @@ public class ApnsPayloadBuilder {
      */
     public ApnsPayloadBuilder setLocalizedAlertTitle(final String localizedAlertTitleKey, final String... alertTitleArguments) {
         this.localizedAlertTitleKey = localizedAlertTitleKey;
-        this.localizedAlertTitleArguments = alertTitleArguments.length > 0 ? alertTitleArguments : null;
+        this.localizedAlertTitleArguments = (alertTitleArguments != null && alertTitleArguments.length > 0) ? alertTitleArguments : null;
 
         this.alertTitle = null;
 
@@ -250,7 +250,7 @@ public class ApnsPayloadBuilder {
      */
     public ApnsPayloadBuilder setLocalizedAlertSubtitle(final String localizedAlertSubtitleKey, final String... alertSubtitleArguments) {
         this.localizedAlertSubtitleKey = localizedAlertSubtitleKey;
-        this.localizedAlertSubtitleArguments = alertSubtitleArguments.length > 0 ? alertSubtitleArguments : null;
+        this.localizedAlertSubtitleArguments = (alertSubtitleArguments != null && alertSubtitleArguments.length > 0) ? alertSubtitleArguments : null;
 
         this.alertSubtitle = null;
 


Test patch:
diff --git a/pushy/src/test/java/com/relayrides/pushy/apns/util/ApnsPayloadBuilderTest.java b/pushy/src/test/java/com/relayrides/pushy/apns/util/ApnsPayloadBuilderTest.java
index a1f5fa74..024f0824 100644
--- a/pushy/src/test/java/com/relayrides/pushy/apns/util/ApnsPayloadBuilderTest.java
+++ b/pushy/src/test/java/com/relayrides/pushy/apns/util/ApnsPayloadBuilderTest.java
@@ -108,6 +108,9 @@ public class ApnsPayloadBuilderTest {
             assertNull(alert.get("body"));
         }
 
+        // We're happy here as long as nothing explodes
+        this.builder.setLocalizedAlertMessage(alertKey, null);
+
         final String[] alertArgs = new String[] { "Moose", "helicopter" };
         this.builder.setLocalizedAlertMessage(alertKey, alertArgs);
 
@@ -121,7 +124,6 @@ public class ApnsPayloadBuilderTest {
             assertEquals(alertArgs.length, argsArray.size());
             assertTrue(argsArray.containsAll(java.util.Arrays.asList(alertArgs)));
         }
-
     }
 
     @Test
@@ -161,6 +163,9 @@ public class ApnsPayloadBuilderTest {
             assertNull(alert.get("title"));
         }
 
+        // We're happy here as long as nothing explodes
+        this.builder.setLocalizedAlertTitle(localizedAlertTitleKey, null);
+
         final String[] alertArgs = new String[] { "Moose", "helicopter" };
         this.builder.setLocalizedAlertTitle(localizedAlertTitleKey, alertArgs);
 
@@ -215,6 +220,9 @@ public class ApnsPayloadBuilderTest {
             assertNull(alert.get("subtitle"));
         }
 
+        // We're happy here as long as nothing explodes
+        this.builder.setLocalizedAlertSubtitle(subtitleKey, null);
+
         final String[] subtitleArgs = new String[] { "Moose", "helicopter" };
         this.builder.setLocalizedAlertSubtitle(subtitleKey, subtitleArgs);