{"record":{"id":"22a6268f0e73e71d","repo":"Yalantis/uCrop","slug":"draw-triangle-one-of-the-specified-color-is-nu","errorCode":null,"errorMessage":"draw_triangle(): One of the specified color is (null).","messagePattern":"draw_triangle\\(\\): One of the specified color is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":50998,"sourceCode":"       \\param x2 X-coordinate of the third vertex in the image instance.\n       \\param y2 Y-coordinate of the third vertex in the image instance.\n       \\param color1 Pointer to \\c spectrum() consecutive values of type \\c T, defining the color of the first vertex.\n       \\param color2 Pointer to \\c spectrum() consecutive values of type \\c T, defining the color of the second vertex.\n       \\param color3 Pointer to \\c spectrum() consecutive values of type \\c T, defining the color of the third vertex.\n       \\param opacity Drawing opacity.\n     **/\n    template<typename tc>\n    CImg<T>& draw_triangle(int x0, int y0,\n                           int x1, int y1,\n                           int x2, int y2,\n                           const tc *color0,\n                           const tc *color1,\n                           const tc *color2,\n                           const float opacity=1) {\n      typedef typename cimg::superset<tc,int>::type stc;\n      if (is_empty()) return *this;\n      if (!color0 || !color1 || !color2)\n        throw CImgArgumentException(_cimg_instance\n                                    \"draw_triangle(): One of the specified color is (null).\",\n                                    cimg_instance);\n\n      if (y0>y1) cimg::swap(x0,x1,y0,y1,color0,color1);\n      if (y0>y2) cimg::swap(x0,x2,y0,y2,color0,color2);\n      if (y1>y2) cimg::swap(x1,x2,y1,y2,color1,color2);\n      if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;\n\n      const int w1 = width() - 1, h1 = height() - 1, cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1);\n      const longT\n        dx01 = (longT)x1 - x0, dx02 = (longT)x2 - x0, dx12 = (longT)x2 - x1,\n        dy01 = std::max((longT)1,(longT)y1 - y0),\n        dy02 = std::max((longT)1,(longT)y2 - y0),\n        dy12 = std::max((longT)1,(longT)y2 - y1),\n        hdy01 = dy01/2, hdy02 = dy02/2, hdy12 = dy12/2;\n\n      cimg_init_scanline(opacity);\n","sourceCodeStart":50980,"sourceCodeEnd":51016,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L50980-L51016","documentation":"This draw_triangle() overload takes three separate per-vertex colors (color0, color1, color2) for gradient shading. All three must be non-null; any null among them makes interpolation impossible, so the library throws CImgArgumentException naming the problem. The check happens before vertex sorting/swapping so no partial work is done.","triggerScenarios":"Calling draw_triangle(x0,y0,x1,y1,x2,y2,color0,color1,color2,opacity) with any of the three color pointers NULL; passing data() of an empty CImg for one vertex color.","commonSituations":"Arrays of color pointers where one entry was never set; partially initialized color list from config; JNI code filling only some vertex colors.","solutions":["Ensure all three per-vertex color arrays are allocated and non-null before the call","Validate each pointer: if (c0 && c1 && c2) img.draw_triangle(...); else supply defaults","Replace the null color with a copy of a neighboring vertex color as a fallback","Fix the upstream initialization loop that leaves one color unset"],"exampleFix":"// before\nconst float *cols[3] = {c0, c1, NULL};\nimg.draw_triangle(x0,y0,x1,y1,x2,y2,cols[0],cols[1],cols[2],opacity);\n// after\nconst float c2[3] = {0,0,255};\nconst float *cols[3] = {c0, c1, c2}; // all non-null\nimg.draw_triangle(x0,y0,x1,y1,x2,y2,cols[0],cols[1],cols[2],opacity);","handlingStrategy":"validation","validationCode":"if (color0 && color1 && color2 && !img.is_empty()) { img.draw_triangle(x0,y0,x1,y1,x2,y2,color0,color1,color2,opacity); }","typeGuard":"bool allColorsValid(const tc* c0, const tc* c1, const tc* c2) { return c0 && c1 && c2; }","tryCatchPattern":"try { img.draw_triangle(x0,y0,x1,y1,x2,y2,c0,c1,c2,opacity); } catch (const CImgArgumentException& e) { /* substitute default vertex colors and retry */ }","preventionTips":["Initialize every per-vertex color in loops; avoid partially filled arrays","Use std::array<const tc*,3> and validate all entries before drawing","Default missing vertex colors to a neighbor's color instead of null","In JNI, check each element of a color-pointer array for null"],"tags":["cimg","null-pointer","argument-validation","gradient"],"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"}