{"id":"04e5490b8fd7b5e1","repo":"square/retrofit","slug":"scheduler-null-04e549","errorCode":null,"errorMessage":"scheduler == null","messagePattern":"scheduler == null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"retrofit-adapters/rxjava2/src/main/java/retrofit2/adapter/rxjava2/RxJava2CallAdapterFactory.java","lineNumber":81,"sourceCode":"   * Returns an instance which creates synchronous observables that do not operate on any scheduler\n   * by default.\n   */\n  public static RxJava2CallAdapterFactory create() {\n    return new RxJava2CallAdapterFactory(null, false);\n  }\n\n  /** Returns an instance which creates asynchronous observables. */\n  public static RxJava2CallAdapterFactory createAsync() {\n    return new RxJava2CallAdapterFactory(null, true);\n  }\n\n  /**\n   * Returns an instance which creates synchronous observables that {@linkplain\n   * Observable#subscribeOn(Scheduler) subscribe on} {@code scheduler} by default.\n   */\n  @SuppressWarnings(\"ConstantConditions\") // Guarding public API nullability.\n  public static RxJava2CallAdapterFactory createWithScheduler(Scheduler scheduler) {\n    if (scheduler == null) throw new NullPointerException(\"scheduler == null\");\n    return new RxJava2CallAdapterFactory(scheduler, false);\n  }\n\n  private final @Nullable Scheduler scheduler;\n  private final boolean isAsync;\n\n  private RxJava2CallAdapterFactory(@Nullable Scheduler scheduler, boolean isAsync) {\n    this.scheduler = scheduler;\n    this.isAsync = isAsync;\n  }\n\n  @Override\n  public @Nullable CallAdapter<?, ?> get(\n      Type returnType, Annotation[] annotations, Retrofit retrofit) {\n    Class<?> rawType = getRawType(returnType);\n\n    if (rawType == Completable.class) {\n      // Completable is not parameterized (which is what the rest of this method deals with) so it","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/square/retrofit/blob/d0b112dad073b7fe49c953ebc46ff1b424cb1e51/retrofit-adapters/rxjava2/src/main/java/retrofit2/adapter/rxjava2/RxJava2CallAdapterFactory.java#L63-L99","documentation":"`RxJava2CallAdapterFactory.createWithScheduler(Scheduler)` at RxJava2CallAdapterFactory.java:81 requires a non-null Scheduler because every stream produced will subscribeOn it. A null value would NPE deep inside RxJava 2 internals, so the factory fails fast at construction with a clear message.","triggerScenarios":"Calling `RxJava2CallAdapterFactory.createWithScheduler(null)` while building the Retrofit instance.","commonSituations":"Passing an uninitialized Scheduler field; resolving a scheduler from config that came back null; or refactoring from create() and forgetting the argument.","solutions":["Pass a concrete Scheduler: `RxJava2CallAdapterFactory.createWithScheduler(io.reactivex.schedulers.Schedulers.io())`.","If you do not need a default scheduler, use `create()` or `createAsync()`.","Initialize the scheduler before constructing the factory."],"exampleFix":"// before\nRetrofit r = new Retrofit.Builder()\n    .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(resolveScheduler()))\n    .build(); // null -> NPE\n\n// after\nScheduler s = resolveScheduler();\nif (s == null) s = io.reactivex.schedulers.Schedulers.io();\nRetrofit r = new Retrofit.Builder()\n    .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(s))\n    .build();","handlingStrategy":"validation","validationCode":"Scheduler s = resolveSchedulerFromConfig();\nif (s == null) throw new IllegalArgumentException(\"scheduler config missing\");\nRxJava2CallAdapterFactory factory = RxJava2CallAdapterFactory.createWithScheduler(s);","typeGuard":null,"tryCatchPattern":"RxJava2CallAdapterFactory factory;\ntry {\n    factory = RxJava2CallAdapterFactory.createWithScheduler(resolveScheduler());\n} catch (NullPointerException e) {\n    if (e.getMessage().equals(\"scheduler == null\")) {\n        factory = RxJava2CallAdapterFactory.create();\n    } else throw e;\n}","preventionTips":["Validate scheduler config before factory construction.","Prefer create()/createAsync() unless you need subscribeOn by default.","Unit-test your wiring to catch null schedulers."],"tags":["retrofit","rxjava2","java","null-guard","scheduler","config"],"analyzedSha":"d0b112dad073b7fe49c953ebc46ff1b424cb1e51","analyzedAt":"2026-08-04T19:12:59.096Z","schemaVersion":2}