Skip to content

Commit fc5979d

Browse files
committed
#14 Bugfix: Fix NullPointerException when server has disconnected.
1 parent 497ac40 commit fc5979d

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

andlinker/src/main/java/com/codezjx/andlinker/adapter/DefaultCallAdapterFactory.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,36 @@ public class DefaultCallAdapterFactory extends CallAdapter.Factory {
1414
public static final CallAdapter.Factory INSTANCE = new DefaultCallAdapterFactory();
1515

1616
@Override
17-
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations) {
17+
public CallAdapter<?, ?> get(final Type returnType, Annotation[] annotations) {
1818
return new CallAdapter<Object, Object>() {
1919
@Override
2020
public Object adapt(Call<Object> call) {
21+
Class<?> rawType = getRawType(returnType);
22+
Object result = call.execute();
23+
if (result == null) {
24+
result = createDefaultResult(rawType);
25+
}
2126
// Return the result
22-
return call.execute();
27+
return result;
2328
}
2429
};
2530
}
31+
32+
private Object createDefaultResult(Class<?> returnType) {
33+
// For java.lang.NullPointerException: Expected to unbox a 'xxx' primitive type but was returned null
34+
// Visit https://github.com/codezjx/AndLinker/issues/14
35+
if (returnType == byte.class) {
36+
return (byte) 0;
37+
} else if (returnType == short.class) {
38+
return (short) 0;
39+
} else if (returnType == int.class || returnType == long.class || returnType == float.class || returnType == double.class) {
40+
return 0;
41+
} else if (returnType == boolean.class) {
42+
return false;
43+
} else if (returnType == char.class) {
44+
return ' ';
45+
}
46+
return null;
47+
}
2648

2749
}

0 commit comments

Comments
 (0)