pentaho/pentaho-kettle · error · RhinoRuntimeError

The function call replace is not valid (wrong number of…

Error message

The function call replace is not valid (wrong number of arguments)

What it means

The ScriptValuesMod `replace(str, search1, repl1, ...)` function requires an odd number of arguments (the source string plus complete search/replacement pairs). It throws this runtime error when `ArgList.length` is even or < 3, i.e. there is no valid search/replace pair.

Solutions

  1. Pass an odd number of arguments: replace(source, search1, replacement1[, search2, replacement2, ...])
  2. Ensure every search value has a matching replacement value
  3. For single search/replace, call: replace(field, 'old', 'new')

Example fix

// before
var out = replace(myField, '-');
// after
var out = replace(myField, '-', '_');
Defensive patterns

Strategy: validation

Validate before calling

if (arguments.length < 3 || arguments.length % 2 === 0) {
  throw new Error('replace requires (str, search1, repl1[, search2, repl2...])');
}
var out = replace(str, 'a', 'b');

Type guard

function isReplaceSafe(args) { return args.length >= 3 && args.length % 2 === 1; }

Prevention

When it happens

Trigger: Calling replace() with 0, 1, 2, 4... arguments, e.g. `replace(str)` or `replace(str, 'a')` (a search value with no replacement).

Common situations: Forgetting the replacement string; using regex-style replace() with only a pattern; copy-paste from JavaScript docs where replace takes 2 args but this variant takes 3+ (odd count).

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/b4117e4803e21383. Report an issue: GitHub.

Appendix: source

Thrown at engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java:1557

  }

  public static String replace( Context actualContext, Scriptable actualObject, Object[] ArgList,
    Function FunctionContext ) {
    try {
      if ( ArgList.length >= 2 && ( ArgList.length - 1 ) % 2 == 0 ) {
        if ( isNull( ArgList, new int[] { 0, 1 } ) ) {
          return null;
        } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {
          return (String) Context.getUndefinedValue();
        }
        String objForReplace = Context.toString( ArgList[0] );
        for ( int i = 1; i < ArgList.length - 1; i = i + 2 ) {
          objForReplace =
            objForReplace.replaceAll( Context.toString( ArgList[i] ), Context.toString( ArgList[i + 1] ) );
        }
        return objForReplace;
      } else {
        throw Context.reportRuntimeError( "The function call replace is not valid (wrong number of arguments)" );
      }
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( "Function call replace is not valid : " + e.getMessage() );
    }
  }

  // Implementation of the JS AlertBox
  public static String Alert( Context actualContext, Scriptable actualObject, Object[] ArgList,
    Function FunctionContext ) {

    SpoonInterface spoon = SpoonFactory.getInstance();
    if ( ArgList.length == 1 && spoon != null ) {
      String strMessage = Context.toString( ArgList[0] );
      boolean ok = spoon.messageBox( strMessage, "Alert", true, Const.INFO );
      if ( !ok ) {
        throw new RuntimeException( "Alert dialog cancelled by user." );
      }
    }

View on GitHub (pinned to f3058517a1)