Skip to content

Commit

Permalink
#14 Bugfix: Fix NullPointerException when server has disconnected.
Browse files Browse the repository at this point in the history
  • Loading branch information
codezjx committed Apr 19, 2022
1 parent 497ac40 commit fc5979d
Showing 1 changed file with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object, Object>() {
@Override
public Object adapt(Call<Object> 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;
}

}

0 comments on commit fc5979d

Please sign in to comment.