Yalantis/uCrop · error · CImgArgumentException

[cimg_appname_math_parser] CImg<%s>::%s: %s: Array brackets

Error message

[cimg_appname_math_parser] CImg<%s>::%s: %s: Array brackets used on non-vector variable '%s', in expression '%s'.

What it means

Square-bracket element access 'name[...]' is only valid on vector variables. When the compiler detects that the base name compiles to a scalar (is_scalar(arg1)), it rejects array-bracket usage with this error.

Source

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

              if (!imglist) _cimg_mp_return(0);
              pos = scalar3(is_relative?mp_list_joff:mp_list_ioff,p1,arg1,arg2==~0U?_cimg_mp_boundary:arg2);
            } else {
              if (!imgin) _cimg_mp_return(0);
              need_input_copy = true;
              pos = scalar2(is_relative?mp_joff:mp_ioff,arg1,arg2==~0U?_cimg_mp_boundary:arg2);
            }
            memtype[pos] = -1; // Prevent from being used in further optimization
            _cimg_mp_return(pos);
          }

          s0 = se1; while (s0>ss && (*s0!='[' || level[s0 - expr._data]!=clevel)) --s0;
          if (s0>ss) { // Vector element
            arg1 = compile(ss,s0,depth1,0,block_flags);
            if (is_scalar(arg1)) {
              variable_name.assign(ss,(unsigned int)(s0 - ss + 1)).back() = 0;
              cimg::strellipsize(variable_name,64);
              _cimg_mp_strerr;
              throw CImgArgumentException("[" cimg_appname "_math_parser] "
                                          "CImg<%s>::%s: %s: Array brackets used on non-vector variable '%s', "
                                          "in expression '%s'.",
                                          pixel_type(),_cimg_mp_calling_function,s_op,
                                          variable_name._data,s0);
            }
            s1 = s0 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;

            if (s1<se1) { // Two or three arguments -> sub-vector extraction
              p1 = size(arg1);
              arg2 = compile(++s0,s1,depth1,0,block_flags); // Starting index
              s0 = ++s1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
              arg3 = compile(s1,s0,depth1,0,block_flags); // Length
              arg4 = s0<se1?compile(++s0,se1,depth1,0,block_flags):1; // Step
              _cimg_mp_check_const_scalar(arg3,2,3);
              arg3 = (unsigned int)mem[arg3];
              pos = vector(arg3);
              CImg<ulongT>::vector((ulongT)mp_vector_crop,pos,arg1,p1,arg2,arg3,arg4).move_to(code);
              return_comp = true;

View on GitHub (pinned to f788b534b4)

Solutions

  1. Define the base as a vector first (e.g. 'x = vector(4)')
  2. Remove the bracket indexing if the value is scalar
  3. Rename variables to avoid shadowing a vector with a scalar

Example fix

// before
x = 3; y = x[0];
// after
x = vector(1,3); y = x[0];
Defensive patterns

Strategy: type-guard

Validate before calling

// Track scalar vs vector variables in your expression
function expectsVector(name, vectorVars) {
  return vectorVars.has(name); // only index names known to be vectors
}

Type guard

function isVectorVar(name, declared) {
  return declared.get(name) === 'vector';
}
// use: if (!isVectorVar('x', decl)) throw new Error('x is scalar, cannot index');

Try / catch

try {
  img.evaluate(expr);
} catch (e) {
  if (/Array brackets used on non-vector/.test(String(e))) {
    // define the base as a vector or drop indexing
  } else throw e;
}

Prevention

When it happens

Trigger: Using 'x[2]' where x is a scalar variable or scalar constant rather than a vector, inside a math-parser expression.

Common situations: Confusing scalar variables with vectors, assuming all variables support element indexing, porting code where x used to be a vector.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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