Yalantis/uCrop · error · CImgArgumentException

load_bmp(): Specified filename is (null).

Error message

load_bmp(): Specified filename is (null).

What it means

Raised by the internal _load_bmp() guard when both the std::FILE* argument and the filename string are null, meaning the caller gave no data source at all (the '(null)' in the message is how CImg prints the missing filename). It is a generic input-validation sentinel: the faulting input is the null filename/file handle. Pass either an open FILE* or a valid filename.

Source

Thrown at ucrop/src/main/jni/CImg.h:56774

    //! Load image from a BMP file \newinstance.
    static CImg<T> get_load_bmp(const char *const filename) {
      return CImg<T>().load_bmp(filename);
    }

    //! Load image from a BMP file \overloading.
    CImg<T>& load_bmp(std::FILE *const file) {
      return _load_bmp(file,0);
    }

    //! Load image from a BMP file \newinstance.
    static CImg<T> get_load_bmp(std::FILE *const file) {
      return CImg<T>().load_bmp(file);
    }

    CImg<T>& _load_bmp(std::FILE *const file, const char *const filename) {
      if (!file && !filename)
        throw CImgArgumentException(_cimg_instance
                                    "load_bmp(): Specified filename is (null).",
                                    cimg_instance);

      const ulongT fsiz = (ulongT)(file?cimg::fsize(file):cimg::fsize(filename));
      std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
      CImg<ucharT> header(54);
      cimg::fread(header._data,54,nfile);
      if (*header!='B' || header[1]!='M') {
        if (!file) cimg::fclose(nfile);
        throw CImgIOException(_cimg_instance
                              "load_bmp(): Invalid BMP file '%s'.",
                              cimg_instance,
                              filename?filename:"(FILE*)");
      }

      // Read header and pixel buffer.
      int
        file_size = header[0x02] + (header[0x03]<<8) + (header[0x04]<<16) + (header[0x05]<<24),

View on GitHub (pinned to f788b534b4)

Solutions

  1. Pass a valid filename or FILE* to load_bmp.
  2. Null-check the path before the call and surface a domain-specific error.
  3. Use load() with the filename and let CImg dispatch by extension/magic if format is uncertain.

Example fix

// before
img.load_bmp(file); // file==nullptr and filename==nullptr
// after
if (filename) img.load_bmp(filename);
else if (file) img.load_bmp(file);
else throw std::runtime_error("no BMP source");
Defensive patterns

Strategy: type-guard

Validate before calling

if (!path && !file) throw std::invalid_argument("load_bmp requires a filename or FILE*");

Type guard

bool hasBmpSource(std::FILE* f, const char* name){ return f != nullptr || name != nullptr; }

Try / catch

try { img.load_bmp(path); } catch (cimg_library::CImgArgumentException& e) { /* null source */ }

Prevention

When it happens

Trigger: Calling load_bmp(nullptr) or the file/filename dispatch overload with both arguments NULL.

Common situations: Path variable never initialized; upstream code that clears the filename on error then retries the load; misuse of get_load_bmp wrapper chains.

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 Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/847ad02f5112bef8. Report an issue: GitHub.