pentaho/pentaho-kettle · error · RuntimeException

The function call indexOf requires 2 or 3 arguments

Error message

The function call indexOf requires 2 or 3 arguments

What it means

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.

Solutions

  1. Always pass the subject string as the first argument: indexOf(str, sub)
  2. Add a numeric third argument only if you need a start offset
  3. Count arguments in generated/dynamic scripts before invoking

Example fix

// before
var pos = indexOf('needle');
// after
var pos = indexOf(haystack, 'needle');
Defensive patterns

Strategy: validation

Validate before calling

// JS in Script step
if (arguments.length < 2 || arguments.length > 3) {
  throw new Error('indexOf expects (string, substring[, fromIndex])');
}
var pos = indexOf(haystack, needle);

Type guard

// JS
function validIndexOfArgs(args) {
  return args.length === 2 || args.length === 3;
}

Try / catch

try {
  var pos = indexOf(str, sub, from);
} catch (e) {
  Logger.logError('indexOf failed: ' + e.message);
  pos = -1;
}

Prevention

When it happens

Trigger: Calling indexOf(str) with only one argument, or indexOf(str, sub, from, extra) with four arguments, in a Script step.

Common situations: 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'.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of pentaho/pentaho-kettle@f3058517a1 (2026-09-13). Data as JSON: /api/errors/84c871b72fe3d68d. Report an issue: GitHub.

Appendix: source

Thrown at engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java:177

  public static int indexOf( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
    Object FunctionContext ) {

    int returnIndex = -1;

    if ( ArgList.length == 2 || ArgList.length == 3 ) {
      if ( !isNull( ArgList ) && !isUndefined( ArgList ) ) {
        String string = (String) ArgList[0];
        String subString = (String) ArgList[1];

        int fromIndex = 0;
        if ( ArgList.length == 3 ) {
          fromIndex = (int) Math.round( (Double) ArgList[2] );
        }
        returnIndex = string.indexOf( subString, fromIndex );
      }
    } else {
      throw new RuntimeException( "The function call indexOf requires 2 or 3 arguments" );
    }
    return returnIndex;
  }

  public static Object getTransformationName( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
    Object FunctionContext ) {
    try {
      Object objTranName = actualObject.get( "_TransformationName_" );
      return objTranName;
    } catch ( Exception e ) {
      throw new RuntimeException( e.toString() );
    }
  }

  public static void appendToFile( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
    Object FunctionContext ) {

    if ( !isNull( ArgList ) && !isUndefined( ArgList ) ) {

View on GitHub (pinned to f3058517a1)