{"record":{"id":"e0a0a37370e10c47","repo":"Yalantis/uCrop","slug":"save-jpeg-specified-filename-is-null","errorCode":null,"errorMessage":"save_jpeg(): Specified filename is (null).","messagePattern":"save_jpeg\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":61655,"sourceCode":"    }\n\n    //! Save image as a JPEG file.\n    /**\n      \\param filename Filename, as a C-string.\n      \\param quality Image quality (in %)\n    **/\n    const CImg<T>& save_jpeg(const char *const filename, const unsigned int quality=100) const {\n      return _save_jpeg(0,filename,quality);\n    }\n\n    //! Save image as a JPEG file \\overloading.\n    const CImg<T>& save_jpeg(std::FILE *const file, const unsigned int quality=100) const {\n      return _save_jpeg(file,0,quality);\n    }\n\n    const CImg<T>& _save_jpeg(std::FILE *const file, const char *const filename, const unsigned int quality) const {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_jpeg(): Specified filename is (null).\",\n                                    cimg_instance);\n      if (is_empty()) { cimg::fempty(file,filename); return *this; }\n      if (_depth>1)\n        cimg::warn(_cimg_instance\n                   \"save_jpeg(): Instance is volumetric, only the first slice will be saved in file '%s'.\",\n                   cimg_instance,\n                   filename?filename:\"(FILE*)\");\n\n#ifndef cimg_use_jpeg\n      if (!file) return save_other(filename,quality);\n      else throw CImgIOException(_cimg_instance\n                                 \"save_jpeg(): Unable to save data in '(*FILE)' unless libjpeg is enabled.\",\n                                 cimg_instance);\n#else\n      unsigned int dimbuf = 0;\n      J_COLOR_SPACE colortype = JCS_RGB;\n","sourceCodeStart":61637,"sourceCodeEnd":61673,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L61637-L61673","documentation":"CImg's save_jpeg() throws a CImgArgumentException when both the FILE* and the filename pointer are null. The JPEG saving path needs either an open file stream or a path string; with neither, there is no destination and the call cannot proceed.","triggerScenarios":"Calling img.save_jpeg((std::FILE*)nullptr) (the FILE*-only overload) with a null pointer, or invoking the internal _save_jpeg with file==nullptr and filename==nullptr.","commonSituations":"Programmatically constructing the destination from a variable that turned out to be null — e.g. a FILE* from a failed fopen passed straight to save_jpeg, or an optional filename that was never assigned.","solutions":["Pass a valid filename string: img.save_jpeg(\"out.jpg\").","Open the file first with fopen and pass the non-null FILE* to save_jpeg(file).","Guard the call site: check the FILE*/filename for null before calling and fail early with your own error."],"exampleFix":"// before\nstd::FILE *f = fopen(path, \"wb\"); // may be null\nimg.save_jpeg(f);\n// after\nstd::FILE *f = fopen(path, \"wb\");\nif (!f) throw std::runtime_error(\"cannot open \" + std::string(path));\nimg.save_jpeg(f);\nfclose(f);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"bool validDest(const std::FILE *f, const char *name) {\n  return f != nullptr || (name != nullptr && *name != '\\0');\n}","tryCatchPattern":"try {\n  img.save_jpeg(file, quality);\n} catch (const cimg_library::CImgArgumentException &e) {\n  std::fprintf(stderr, \"jpeg dest missing: %s\\n\", e.what());\n}","preventionTips":["Never pass a FILE* from unchecked fopen directly to save_jpeg.","Check fopen result and filename initialization before saving.","Prefer filename-based overloads to avoid null-handle issues."],"tags":["cimg","image","jpeg","null-argument"],"backgroundTag":"null-argument","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}