{"id":"a7486d9cd1f11bc2","repo":"square/retrofit","slug":"scheduler-null","errorCode":null,"errorMessage":"scheduler == null","messagePattern":"scheduler == null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"retrofit-adapters/rxjava/src/main/java/retrofit2/adapter/rxjava/RxJavaCallAdapterFactory.java","lineNumber":83,"sourceCode":"   * Returns an instance which creates synchronous observables that do not operate on any scheduler\n   * by default.\n   */\n  public static RxJavaCallAdapterFactory create() {\n    return new RxJavaCallAdapterFactory(null, false);\n  }\n\n  /** Returns an instance which creates asynchronous observables. */\n  public static RxJavaCallAdapterFactory createAsync() {\n    return new RxJavaCallAdapterFactory(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 RxJavaCallAdapterFactory createWithScheduler(Scheduler scheduler) {\n    if (scheduler == null) throw new NullPointerException(\"scheduler == null\");\n    return new RxJavaCallAdapterFactory(scheduler, false);\n  }\n\n  private final @Nullable Scheduler scheduler;\n  private final boolean isAsync;\n\n  private RxJavaCallAdapterFactory(@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    boolean isSingle = rawType == Single.class;\n    boolean isCompletable = rawType == Completable.class;\n    if (rawType != Observable.class && !isSingle && !isCompletable) {","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/square/retrofit/blob/d0b112dad073b7fe49c953ebc46ff1b424cb1e51/retrofit-adapters/rxjava/src/main/java/retrofit2/adapter/rxjava/RxJavaCallAdapterFactory.java#L65-L101","documentation":"`RxJavaCallAdapterFactory.createWithScheduler(Scheduler)` at RxJavaCallAdapterFactory.java:83 requires a non-null Scheduler because the factory will subscribeOn the supplied scheduler for every stream produced. A null scheduler would NPE later inside RxJava internals with a far less obvious message, so the factory fails fast at construction.","triggerScenarios":"Calling `RxJavaCallAdapterFactory.createWithScheduler(null)` when building the Retrofit instance.","commonSituations":"Passing a Scheduler field that has not been initialized yet; loading a scheduler name from config, resolving to null; or refactoring from create() to createWithScheduler() and forgetting to supply an argument.","solutions":["Pass a concrete Scheduler, e.g. `RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())`.","If you do not need a default scheduler, use `create()` (synchronous) or `createAsync()` (async) instead.","Initialize the scheduler field before constructing the factory."],"exampleFix":"// before\nRetrofit r = new Retrofit.Builder()\n    .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(resolveScheduler()))\n    .build(); // resolveScheduler() may return null -> NPE\n\n// after\nScheduler s = resolveScheduler();\nif (s == null) s = rx.schedulers.Schedulers.io();\nRetrofit r = new Retrofit.Builder()\n    .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(s))\n    .build();","handlingStrategy":"validation","validationCode":"// Validate before passing to the factory:\nScheduler s = resolveSchedulerFromConfig();\nif (s == null) throw new IllegalArgumentException(\"scheduler config missing; defaulting is disabled\");\nRxJavaCallAdapterFactory factory = RxJavaCallAdapterFactory.createWithScheduler(s);","typeGuard":null,"tryCatchPattern":"// Wrap factory construction; default to create() if no scheduler:\nRxJavaCallAdapterFactory factory;\ntry {\n    factory = RxJavaCallAdapterFactory.createWithScheduler(resolveScheduler());\n} catch (NullPointerException e) {\n    if (e.getMessage().equals(\"scheduler == null\")) {\n        factory = RxJavaCallAdapterFactory.create(); // synchronous fallback\n    } else throw e;\n}","preventionTips":["Resolve and validate schedulers from config before factory construction.","Prefer create()/createAsync() unless you specifically need subscribeOn by default.","Unit-test your Retrofit wiring module to fail fast on null scheduler."],"tags":["retrofit","rxjava","java","null-guard","scheduler","config"],"analyzedSha":"d0b112dad073b7fe49c953ebc46ff1b424cb1e51","analyzedAt":"2026-08-04T19:12:59.096Z","schemaVersion":2}