pentaho/pentaho-kettle · error · RuntimeException

The function call substr requires 2 or 3 arguments.

Error message

The function call substr requires 2 or 3 arguments.

What it means

Arity guard for substr: the function was called with an argument count other than 2 or 3 (string + start, or string + start + length). The implementation only handles those two signatures; anything else reaches the final else-branch and throws.

Solutions

  1. Call with substr(str, start) or substr(str, start, len).
  2. Remember indices/lengths are numbers: substr(field, 0, 5).
  3. Remove accidental extra arguments from the call.

Example fix

// before
var s = substr(str);
// after
var s = substr(str, 0, 5);
Defensive patterns

Strategy: validation

Validate before calling

if (arguments.length === 2 || arguments.length === 3) { s = substr.apply(null, arguments); } else { s = ''; }

Type guard

function hasValidSubstrArity(args){ return args.length === 2 || args.length === 3; }

Try / catch

try { s = substr(str, start, len); } catch (e) { if (/requires 2 or 3 arguments/.test(e.message)) s = ''; else throw e; }

Prevention

When it happens

Trigger: Calling substr(str) with one argument, substr() with none, or substr(str, start, len, extra) with more than three arguments.

Common situations: Porting code that used substring(begin,end) and adding a stray argument; forgetting the start index; copy-paste leaving a trailing comma creating an extra argument.

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

Appendix: source

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

        if ( from < 0 ) {
          throw new RuntimeException( "start smaller than 0" );
        }
        if ( len < 0 ) {
          len = 0; // Make it compatible with Javascript substr
        }

        to = from + len;
        strLen = sRC.length();
        if ( to > strLen ) {
          to = strLen;
        }
        sRC = sRC.substring( from, to );
      } catch ( Exception e ) {
        throw new RuntimeException( "The function call substr is not valid : " + e.getMessage() );
      }
    } else {
      throw new RuntimeException( "The function call substr requires 2 or 3 arguments." );
    }
    return sRC;
  }

  // Resolve an IP address
  public static String resolveIP( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
    Object FunctionContext ) {
    String sRC = "";
    if ( ArgList.length == 2 ) {
      try {
        InetAddress addr = InetAddress.getByName( (String) ArgList[0] );
        if ( ( (String) ArgList[1] ).equals( "IP" ) ) {
          sRC = addr.getHostName();
        } else {
          sRC = addr.getHostAddress();
        }
        if ( sRC.equals( ArgList[0] ) ) {
          sRC = "-";

View on GitHub (pinned to f3058517a1)