Yalantis/uCrop · error · CImgArgumentException

load_png(): Specified filename is (null).

Error message

load_png(): Specified filename is (null).

What it means

CImg's _load_png() throws CImgArgumentException when both the FILE* handle and the filename string are null, because there is no input source from which to read a PNG image. It is a programmer-error guard at the top of the loader, not an I/O problem.

Solutions

  1. Pass a valid filename: image.load_png("photo.png").
  2. Or pass an already-open FILE*: image.load_png(file_ptr).
  3. Check that the source of the filename string (config, argv, getenv) actually returned a non-null value before calling.

Example fix

// before
const char *name = getenv("IMG");
img.load_png(name); // name may be NULL -> throws
// after
const char *name = getenv("IMG");
if (!name) name = "default.png";
img.load_png(name);
Defensive patterns

Strategy: validation

Validate before calling

if (!filename && !file) throw std::invalid_argument("load_png requires a filename or FILE*");
img.load_png(filename);

Type guard

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

Prevention

When it happens

Trigger: Calling load_png(nullptr) / load_png((const char*)0) directly, or a wrapper (e.g. load_png(file, bits) with a NULL filename and NULL file) that forwards both as null into _load_png.

Common situations: A filename variable was never initialized (declared but not assigned), a caller passes the result of a function that returned NULL (e.g. getenv("PATH_TO_IMAGE") when unset), or string storage was freed before the call.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/8c9b24e65428f2dc. Report an issue: GitHub.

Appendix: source

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

    //! Load image from a PNG file \newinstance.
    static CImg<T> get_load_png(const char *const filename, unsigned int *const bits_per_value=0) {
      return CImg<T>().load_png(filename,bits_per_value);
    }

    //! Load image from a PNG file \overloading.
    CImg<T>& load_png(std::FILE *const file, unsigned int *const bits_per_value=0) {
      return _load_png(file,0,bits_per_value);
    }

    //! Load image from a PNG file \newinstance.
    static CImg<T> get_load_png(std::FILE *const file, unsigned int *const bits_per_value=0) {
      return CImg<T>().load_png(file,bits_per_value);
    }

    // (Note: Most of this function has been written by Eric Fausett).
    CImg<T>& _load_png(std::FILE *const file, const char *const filename, unsigned int *const bits_per_value) {
      if (!file && !filename)
        throw CImgArgumentException(_cimg_instance
                                    "load_png(): Specified filename is (null).",
                                    cimg_instance);

#ifndef cimg_use_png
      cimg::unused(bits_per_value);
      if (file)
        throw CImgIOException(_cimg_instance
                              "load_png(): Unable to load data from '(FILE*)' unless libpng is enabled.",
                              cimg_instance);

      else return load_other(filename);
#else
      // Open file and check for PNG validity.
#if defined __GNUC__
      const char *volatile nfilename = filename; // Use 'volatile' to avoid (wrong) g++ warning
      std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,"rb");
#else
      const char *nfilename = filename;

View on GitHub (pinned to f788b534b4)