{"record":{"id":"84c871b72fe3d68d","repo":"pentaho/pentaho-kettle","slug":"the-function-call-indexof-requires-2-or-3-arguments","errorCode":null,"errorMessage":"The function call indexOf requires 2 or 3 arguments","messagePattern":"The function call indexOf requires 2 or 3 arguments","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":177,"sourceCode":"\n  public static int indexOf( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,\n    Object FunctionContext ) {\n\n    int returnIndex = -1;\n\n    if ( ArgList.length == 2 || ArgList.length == 3 ) {\n      if ( !isNull( ArgList ) && !isUndefined( ArgList ) ) {\n        String string = (String) ArgList[0];\n        String subString = (String) ArgList[1];\n\n        int fromIndex = 0;\n        if ( ArgList.length == 3 ) {\n          fromIndex = (int) Math.round( (Double) ArgList[2] );\n        }\n        returnIndex = string.indexOf( subString, fromIndex );\n      }\n    } else {\n      throw new RuntimeException( \"The function call indexOf requires 2 or 3 arguments\" );\n    }\n    return returnIndex;\n  }\n\n  public static Object getTransformationName( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,\n    Object FunctionContext ) {\n    try {\n      Object objTranName = actualObject.get( \"_TransformationName_\" );\n      return objTranName;\n    } catch ( Exception e ) {\n      throw new RuntimeException( e.toString() );\n    }\n  }\n\n  public static void appendToFile( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,\n    Object FunctionContext ) {\n\n    if ( !isNull( ArgList ) && !isUndefined( ArgList ) ) {","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L159-L195","documentation":"Arity validation guard inside the scripting bridge: indexOf is a user-defined helper exposed to Script/ScriptValue steps, and it throws this error at runtime when the script calls indexOf with an argument count other than 2 or 3 — i.e. the call-site in the user's JavaScript passed too few (only the search string) or too many (extra parameters beyond an optional fromIndex) arguments, so the ArgList length check fails and the exception is raised before any string search runs.","triggerScenarios":"Calling indexOf(str) with only one argument, or indexOf(str, sub, from, extra) with four arguments, in a Script step.","commonSituations":"Mistaking it for Java's String.indexOf with 1 arg (str-only form); JavaScript's indexOf(sub, from) 2-arg form used with only one argument because the string is assumed to be 'this'.","solutions":["Always pass the subject string as the first argument: indexOf(str, sub)","Add a numeric third argument only if you need a start offset","Count arguments in generated/dynamic scripts before invoking"],"exampleFix":"// before\nvar pos = indexOf('needle');\n// after\nvar pos = indexOf(haystack, 'needle');","handlingStrategy":"validation","validationCode":"// JS in Script step\nif (arguments.length < 2 || arguments.length > 3) {\n  throw new Error('indexOf expects (string, substring[, fromIndex])');\n}\nvar pos = indexOf(haystack, needle);","typeGuard":"// JS\nfunction validIndexOfArgs(args) {\n  return args.length === 2 || args.length === 3;\n}","tryCatchPattern":"try {\n  var pos = indexOf(str, sub, from);\n} catch (e) {\n  Logger.logError('indexOf failed: ' + e.message);\n  pos = -1;\n}","preventionTips":["Remember the subject string is argument 1, unlike String.prototype.indexOf","Omit fromIndex instead of passing null for the optional third arg","Migrate carefully from native JS indexOf habits"],"tags":["kettle","javascript","arity","indexof"],"backgroundTag":"missing-required-argument","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}