koral--/android-gif-drawable · error · NullPointerException

Source is not set

Error message

Source is not set

What it means

GifDrawableInit.build() requires that an input source was configured via one of the from*() methods. If no source was set, mInputSource is null and build() throws a NullPointerException with the message 'Source is not set'. This is the builder-pattern misuse guard of the library.

Solutions

  1. Call one of the from*() methods on the builder before build(), e.g. new GifDrawableBuilder().from(gifFile).build().
  2. Ensure the from() call is not skipped by conditional logic; restructure so a source is always provided.
  3. Wrap build() in try-catch for NullPointerException as a defensive last resort.

Example fix

// before
GifDrawable drawable = new GifDrawableBuilder().build();
// after
GifDrawable drawable = new GifDrawableBuilder().from(new File("anim.gif")).build();
Defensive patterns

Strategy: validation

Validate before calling

GifDrawableBuilder builder = new GifDrawableBuilder(); // ... ensure from() called
if (builderSourceSet) { GifDrawable d = builder.build(); }

Try / catch

try { GifDrawable d = builder.build(); } catch (NullPointerException e) { /* source not set — configure from() */ }

Prevention

When it happens

Trigger: Calling GifDrawableBuilder().build() (or another GifDrawableInit subclass) without ever calling from(file/stream/resource/asset/byteBuffer/uri/etc.).

Common situations: Building the source conditionally (e.g. inside an if branch that never ran); forgetting the from() call when refactoring; constructing the builder in one place and passing it on without configuring it.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of koral--/android-gif-drawable@26ff795f78 (2026-09-10). Data as JSON: /api/errors/34f2080546f81044. Report an issue: GitHub.

Appendix: source

Thrown at android-gif-drawable/src/main/java/pl/droidsonroids/gif/GifDrawableInit.java:65

     * Note that this call will overwrite sample size set previously by {@link #options(GifOptions)}
     *
     * @param sampleSize the sample size
     * @return this builder instance, to chain calls
     */
    public T sampleSize(@IntRange(from = 1, to = Character.MAX_VALUE) final int sampleSize) {
        mOptions.setInSampleSize(sampleSize);
        return self();
    }

    /**
     * Appropriate constructor wrapper. Must be preceded by on of {@code from()} calls.
     *
     * @return new drawable instance
     * @throws IOException when creation fails
     */
    public GifDrawable build() throws IOException {
        if (mInputSource == null) {
            throw new NullPointerException("Source is not set");
        }
        return mInputSource.createGifDrawable(mOldDrawable, mExecutor, mIsRenderingTriggeredOnDraw, mOptions);
    }

    /**
     * Sets drawable to be reused when creating new one.
     *
     * @param drawable drawable to be reused
     * @return this builder instance, to chain calls
     */
    public T with(GifDrawable drawable) {
        mOldDrawable = drawable;
        return self();
    }

    /**
     * Sets thread pool size for rendering tasks.
     * Warning: custom executor set by {@link #taskExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)}

View on GitHub (pinned to 26ff795f78)