Skip to content

Commit ecd4887

Browse files
committed
Fix some NullPointerException on StringUtils.implode method
1 parent 3c98414 commit ecd4887

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ allprojects {
1515
Step 2. Add the dependency
1616
```
1717
dependencies {
18-
compile "com.github.playmoweb:library-android-utils:1.0.2"
18+
compile "com.github.playmoweb:library-android-utils:1.0.3"
1919
}
2020
```
2121

android-utils/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77
minSdkVersion 16
88
targetSdkVersion 27
99
versionCode 1
10-
versionName "1.0.2"
10+
versionName "1.0.3"
1111
}
1212
}
1313

android-utils/src/main/java/com/playmoweb/android/utils/StringUtils.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class StringUtils {
1616

1717
/**
1818
* Returns true if the string is null or 0-length.
19+
*
1920
* @param str the string to be examined
2021
* @return true if str is null or zero length
2122
*/
@@ -50,14 +51,14 @@ public static String implode(final String separator, final Collection<String> da
5051

5152
public static String implode(final String separator, final String... data) {
5253
final StringBuilder sb = new StringBuilder();
53-
for (int i = 0; i < data.length - 1; i++) {
54+
for (int i = 0; i < data.length; i++) {
5455
if (!isEmpty(data[i]) && !data[i].matches(" *")) {//empty string are ""; " "; " "; and so on
56+
if (!isEmpty(sb.toString())) {
57+
sb.append(separator);
58+
}
5559
sb.append(data[i]);
56-
sb.append(separator);
5760
}
5861
}
59-
//data.length - 1 => to not add separator at the end
60-
sb.append(data[data.length - 1].trim());
6162
return sb.toString();
6263
}
6364
}

android-utils/src/test/java/com/playmoweb/android/utils/StringUtilsTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,11 @@ public void testImplodeList() {
3939
public void testImplodeWithNull() {
4040
String result = StringUtils.implode(", ", "test1", null, "test2");
4141
assertEquals("test1, test2", result);
42+
result = StringUtils.implode(", ", "test1", "test2", null);
43+
assertEquals("test1, test2", result);
44+
result = StringUtils.implode(", ", null, "test1", "test2");
45+
assertEquals("test1, test2", result);
46+
result = StringUtils.implode(", ", null, "test1", null, "test2", null);
47+
assertEquals("test1, test2", result);
4248
}
4349
}

0 commit comments

Comments
 (0)