{"record":{"id":"3213ad7522598e8f","repo":"Yalantis/uCrop","slug":"draw-line-specified-color-is-null","errorCode":null,"errorMessage":"draw_line(): Specified color is (null).","messagePattern":"draw_line\\(\\): Specified color is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":49977,"sourceCode":"       \\param y0 Y-coordinate of the starting point.\n       \\param z0 Z-coordinate of the starting point\n       \\param x1 X-coordinate of the ending point.\n       \\param y1 Y-coordinate of the ending point.\n       \\param z1 Z-coordinate of the ending point.\n       \\param color Pointer to \\c spectrum() consecutive values of type \\c T, defining the drawing color.\n       \\param opacity Drawing opacity.\n       \\param pattern An integer whose bits describe the line pattern.\n       \\param init_hatch Tells if a reinitialization of the hash state must be done.\n    **/\n    template<typename tz, typename tc>\n    CImg<T>& draw_line(CImg<tz>& zbuffer,\n                       int x0, int y0, const float z0,\n                       int x1, int y1, const float z1,\n                       const tc *const color, const float opacity=1,\n                       const unsigned int pattern=~0U, const bool init_hatch=true) {\n      if (is_empty() || z0<=0 || z1<=0 || !opacity || !pattern) return *this;\n      if (!color)\n        throw CImgArgumentException(_cimg_instance\n                                    \"draw_line(): Specified color is (null).\",\n                                    cimg_instance);\n      if (!is_sameXY(zbuffer))\n        throw CImgArgumentException(_cimg_instance\n                                    \"draw_line(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have \"\n                                    \"different dimensions.\",\n                                    cimg_instance,\n                                    zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);\n\n      if (std::min(y0,y1)>=height() || std::max(y0,y1)<0 || std::min(x0,x1)>=width() || std::max(x0,x1)<0)\n        return *this;\n      int\n        w1 = width() - 1, h1 = height() - 1,\n        dx01 = x1 - x0, dy01 = y1 - y0;\n      float iz0 = 1/z0, iz1 = 1/z1, diz01 = iz1 - iz0;\n\n      const bool is_horizontal = cimg::abs(dx01)>cimg::abs(dy01);\n      if (is_horizontal) cimg::swap(x0,y0,x1,y1,w1,h1,dx01,dy01);","sourceCodeStart":49959,"sourceCodeEnd":49995,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L49959-L49995","documentation":"This draw_line() overload renders a 3D line into an image with a Z-buffer and requires a color array whose length is at least the image's spectrum (e.g. 3 floats for RGB). If the color pointer is null the library cannot fill the drawn pixels and throws CImgArgumentException immediately after the early-return guards (empty image, z<=0, zero opacity/pattern).","triggerScenarios":"Calling img.draw_line(x0,y0,z0,x1,y1,z1,NULL,opacity) — i.e. passing nullptr/0 as the color argument to the Z-buffered line overload — on a non-empty image with positive z0/z1, non-zero opacity and pattern, since otherwise the function silently returns before the color check.","commonSituations":"Passing a null pointer returned from a failed allocation or lookup of a palette entry; a template deduction mismatch that makes the color argument nullptr; porting code from a texture-based draw_line overload (which takes no color) to the Z-buffered one and leaving the color argument empty; conditional color selection defaulting to null.","solutions":["Pass a valid color array sized to the image spectrum, e.g. `const unsigned char red[] = {255,0,0};` for RGB images.","If color may legitimately be absent, decide the call: skip draw_line or substitute a default color instead of nullptr.","Check the code isn't accidentally calling the texture-based draw_line signature where the 4th/5th ints are texture coordinates, not color."],"exampleFix":"// before\nimg.draw_line(0,0,1,100,100,1,nullptr,1.0f);\n\n// after\nconst unsigned char white[] = {255,255,255};\nimg.draw_line(0,0,1,100,100,1,white,1.0f);","handlingStrategy":"validation","validationCode":"const unsigned char col[] = {255,255,255};\nif (!color) return; // or substitute a default color before calling\nimg.draw_line(x0,y0,z0,x1,y1,z1,col,opacity);","typeGuard":"template<typename tc>\nbool has_color(const tc* c) { return c != nullptr; }","tryCatchPattern":"try {\n  img.draw_line(x0,y0,z0,x1,y1,z1,color,opacity);\n} catch (CImgArgumentException& e) {\n  std::fprintf(stderr, \"draw_line failed: %s\\n\", e.what());\n}","preventionTips":["Define named color arrays (e.g. const unsigned char red[]={255,0,0};) and always pass one.","Check for null before calling any draw_* that takes a color pointer.","Match color length to img.spectrum() (3 for RGB, 4 for RGBA).","Don't mix up the texture-based draw_line signatures with the color-based ones."],"tags":["cimg","null-pointer","graphics","color"],"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"}