diff --git a/andlinker/src/main/java/com/codezjx/andlinker/adapter/DefaultCallAdapterFactory.java b/andlinker/src/main/java/com/codezjx/andlinker/adapter/DefaultCallAdapterFactory.java index cf18138..8e21b57 100644 --- a/andlinker/src/main/java/com/codezjx/andlinker/adapter/DefaultCallAdapterFactory.java +++ b/andlinker/src/main/java/com/codezjx/andlinker/adapter/DefaultCallAdapterFactory.java @@ -14,14 +14,36 @@ public class DefaultCallAdapterFactory extends CallAdapter.Factory { public static final CallAdapter.Factory INSTANCE = new DefaultCallAdapterFactory(); @Override - public CallAdapter get(Type returnType, Annotation[] annotations) { + public CallAdapter get(final Type returnType, Annotation[] annotations) { return new CallAdapter() { @Override public Object adapt(Call call) { + Class rawType = getRawType(returnType); + Object result = call.execute(); + if (result == null) { + result = createDefaultResult(rawType); + } // Return the result - return call.execute(); + return result; } }; } + + private Object createDefaultResult(Class returnType) { + // For java.lang.NullPointerException: Expected to unbox a 'xxx' primitive type but was returned null + // Visit https://github.com/codezjx/AndLinker/issues/14 + if (returnType == byte.class) { + return (byte) 0; + } else if (returnType == short.class) { + return (short) 0; + } else if (returnType == int.class || returnType == long.class || returnType == float.class || returnType == double.class) { + return 0; + } else if (returnType == boolean.class) { + return false; + } else if (returnType == char.class) { + return ' '; + } + return null; + } }