pentaho/pentaho-kettle · error · RuntimeException

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

replace applies a sequence of regex replacement pairs to a string: replace(str, regex1, repl1, regex2, repl2, ...). It requires an odd argument count (the string plus whole regex/replacement pairs); any other count triggers this RuntimeException. Note it also requires more than just the string - pairs are mandatory.

Solutions

  1. Ensure arguments are: 1 string + N complete (regex, replacement) pairs - odd total count greater than 1.
  2. Fix pair parity at the call site.
  3. Remember arguments are Java regexes; escape special characters like '.' correctly.

Example fix

// before
var s = replace(text, 'a', 'b', 'c'); // incomplete pair -> throws
// after
var s = replace(text, 'a', 'b', 'c', '');
Defensive patterns

Strategy: validation

Validate before calling

function safeReplace(s) {
  if (typeof s !== 'string' || (arguments.length - 1) < 2 || (arguments.length - 1) % 2 !== 0) {
    return s;
  }
  return replace.apply(null, arguments);
}

Type guard

function isReplaceCall(args) {
  return typeof args[0] === 'string' && args.length > 2 && (args.length - 1) % 2 === 0;
}

Try / catch

try {
  var s = replace(text, 'a', 'b');
} catch (e) {
  if (String(e.message).indexOf('wrong number of arguments') >= 0) {
    // fix arity: 1 string + complete regex/replacement pairs
  }
}

Prevention

When it happens

Trigger: Calling replace(str) with only the string, or with an even number of trailing arguments so a pair is incomplete (e.g. replace(str, 'a') or replace(str, 'a', 'b', 'c')).

Common situations: Adding a third argument to an existing call and breaking pair parity, thinking replace(str, regex) works like String.replace, mixing up replace/replaceAll semantics.

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

Appendix: source

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

    }
  }

  public static String replace( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
    Object 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) undefinedValue;
        }
        String objForReplace = (String) ArgList[0];
        for ( int i = 1; i < ArgList.length - 1; i = i + 2 ) {
          objForReplace = objForReplace.replaceAll( (String) ArgList[i], (String) ArgList[i + 1] );
        }
        return objForReplace;
      } else {
        throw new RuntimeException( "The function call replace is not valid (wrong number of arguments)" );
      }
    } catch ( Exception e ) {
      throw new RuntimeException( "Function call replace is not valid : " + e.getMessage() );
    }
  }

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

    SpoonInterface spoon = SpoonFactory.getInstance();
    if ( ArgList.length == 1 && spoon != null ) {
      String strMessage = (String) ArgList[0];
      spoon.messageBox( strMessage, "Alert", false, Const.INFO );
    }

    return "";
  }

View on GitHub (pinned to f3058517a1)