{"record":{"id":"8c9b24e65428f2dc","repo":"Yalantis/uCrop","slug":"load-png-specified-filename-is-null","errorCode":null,"errorMessage":"load_png(): Specified filename is (null).","messagePattern":"load_png\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":57361,"sourceCode":"    //! Load image from a PNG file \\newinstance.\n    static CImg<T> get_load_png(const char *const filename, unsigned int *const bits_per_value=0) {\n      return CImg<T>().load_png(filename,bits_per_value);\n    }\n\n    //! Load image from a PNG file \\overloading.\n    CImg<T>& load_png(std::FILE *const file, unsigned int *const bits_per_value=0) {\n      return _load_png(file,0,bits_per_value);\n    }\n\n    //! Load image from a PNG file \\newinstance.\n    static CImg<T> get_load_png(std::FILE *const file, unsigned int *const bits_per_value=0) {\n      return CImg<T>().load_png(file,bits_per_value);\n    }\n\n    // (Note: Most of this function has been written by Eric Fausett).\n    CImg<T>& _load_png(std::FILE *const file, const char *const filename, unsigned int *const bits_per_value) {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"load_png(): Specified filename is (null).\",\n                                    cimg_instance);\n\n#ifndef cimg_use_png\n      cimg::unused(bits_per_value);\n      if (file)\n        throw CImgIOException(_cimg_instance\n                              \"load_png(): Unable to load data from '(FILE*)' unless libpng is enabled.\",\n                              cimg_instance);\n\n      else return load_other(filename);\n#else\n      // Open file and check for PNG validity.\n#if defined __GNUC__\n      const char *volatile nfilename = filename; // Use 'volatile' to avoid (wrong) g++ warning\n      std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,\"rb\");\n#else\n      const char *nfilename = filename;","sourceCodeStart":57343,"sourceCodeEnd":57379,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57343-L57379","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a valid filename: image.load_png(\"photo.png\").","Or pass an already-open FILE*: image.load_png(file_ptr).","Check that the source of the filename string (config, argv, getenv) actually returned a non-null value before calling."],"exampleFix":"// before\nconst char *name = getenv(\"IMG\");\nimg.load_png(name); // name may be NULL -> throws\n// after\nconst char *name = getenv(\"IMG\");\nif (!name) name = \"default.png\";\nimg.load_png(name);","handlingStrategy":"validation","validationCode":"if (!filename && !file) throw std::invalid_argument(\"load_png requires a filename or FILE*\");\nimg.load_png(filename);","typeGuard":"bool hasSource(const char *f, std::FILE *h) { return f != nullptr || h != nullptr; }","tryCatchPattern":null,"preventionTips":["Always initialize filename variables at declaration","Guard getenv results for null before use","Prefer std::string over raw char* so null cannot slip in"],"tags":["cimg","argument-validation","null-pointer"],"backgroundTag":"null-argument","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}