lingochamp/FileDownloader · error · IllegalArgumentException

the provided context must not be null!

Error message

the provided context must not be null!

What it means

FileDownloader.init(Context) (deprecated in favor of setup(Context)) throws IllegalArgumentException when the passed Context is null. The library needs a valid application context to set up its service components.

Solutions

  1. Use a guaranteed application context: context.getApplicationContext().
  2. Prefer the non-deprecated FileDownloader.getImpl().setup(context).
  3. Guard with a null check before init, and initialize in Application.onCreate.
  4. If in a Fragment, use requireContext().getApplicationContext() only when attached.

Example fix

// before
FileDownloader.getImpl().init(getActivity());

// after
Context appContext = getApplicationContext();
if (appContext != null) {
    FileDownloader.getImpl().setup(appContext);
}
Defensive patterns

Strategy: validation

Validate before calling

if (context == null) return; // or fail fast
FileDownloader.getImpl().setup(context.getApplicationContext());

Type guard

boolean hasValidContext(Context c) { return c != null && c.getApplicationContext() != null; }

Try / catch

try { FileDownloader.getImpl().setup(context); } catch (IllegalArgumentException e) { deferInitUntilContextAvailable(); }

Prevention

When it happens

Trigger: Calling FileDownloader.getImpl().init(null) directly, or init(context) where context comes from getActivity()/getContext() returning null (e.g. in a detached fragment).

Common situations: Initializing in a Fragment after detachment; passing a Context field assigned later; calling init before onCreate completes.

Related errors


AI-assisted analysis of lingochamp/FileDownloader@6237a8cac1 (2026-09-08). Data as JSON: /api/errors/2cee705a2105de38. Report an issue: GitHub.

Appendix: source

Thrown at library/src/main/java/com/liulishuo/filedownloader/FileDownloader.java:103

     */
    public static DownloadMgrInitialParams.InitCustomMaker setupOnApplicationOnCreate(
            Application application) {
        final Context context = application.getApplicationContext();
        FileDownloadHelper.holdContext(context);

        DownloadMgrInitialParams.InitCustomMaker customMaker =
                new DownloadMgrInitialParams.InitCustomMaker();
        CustomComponentHolder.getImpl().setInitCustomMaker(customMaker);

        return customMaker;
    }

    /**
     * @deprecated please use {@link #setup(Context)} instead.
     */
    public static void init(final Context context) {
        if (context == null) {
            throw new IllegalArgumentException("the provided context must not be null!");
        }

        setup(context);
    }


    /**
     * @deprecated please using {@link #setupOnApplicationOnCreate(Application)} instead.
     */
    public static void init(final Context context,
                            final DownloadMgrInitialParams.InitCustomMaker maker) {
        if (FileDownloadLog.NEED_LOG) {
            FileDownloadLog.d(FileDownloader.class, "init Downloader with params: %s %s",
                    context, maker);
        }

        if (context == null) {
            throw new IllegalArgumentException("the provided context must not be null!");

View on GitHub (pinned to 6237a8cac1)