pentaho/pentaho-kettle · error · JavaScriptException

The first Argument must be a Number.

Error message

The first Argument must be a Number.

What it means

In the 1-argument form of num2str, the function converts the first argument with Context.toNumber and throws this error if the result is NaN. It means the value passed cannot be interpreted as a number, so no DecimalFormat formatting is possible.

Solutions

  1. Ensure the first argument is numeric: use str2num() first if it is a string, e.g. num2str(str2num(s)).
  2. Verify the field's data type in the transformation (set the field to Number instead of String in the step's field mapping).
  3. Check for NaN upstream: log or test isNaN(value) before formatting.
  4. Strip non-numeric characters or fix the locale-specific formatting of the source string before conversion.

Example fix

// before
var s = num2str(amountStr); // amountStr is a String field

// after
var s = num2str(str2num(amountStr));
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof value === 'number' && !isNaN(value)) { var s = num2str(value); }

Type guard

function isNumber(v) { return typeof v === 'number' && !isNaN(v); }

Prevention

When it happens

Trigger: num2str(x) where x is a non-numeric string (e.g. num2str("abc")), an object without numeric coercion, or a value that Rhino converts to NaN.

Common situations: Passing a String-typed field from the row that actually contains text; reading a field that upstream produced as alphanumeric; locale-formatted numbers with thousands separators that Rhino can't parse as a number; undefined field names resolving to odd values.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

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

  }

  // Converts the given Numeric to a JScript String
  public static String num2str( Context actualContext, Scriptable actualObject, Object[] ArgList,
    Function FunctionContext ) {
    String sRC = "";
    switch ( ArgList.length ) {
      case 0:
        throw Context.reportRuntimeError( "The function call num2str requires at least 1 argument." );
      case 1:
        try {
          if ( isNull( ArgList[0] ) ) {
            return null;
          } else if ( isUndefined( ArgList[0] ) ) {
            return (String) Context.getUndefinedValue();
          }
          double sArg1 = Context.toNumber( ArgList[0] );
          if ( Double.isNaN( sArg1 ) ) {
            throw Context.reportRuntimeError( "The first Argument must be a Number." );
          }
          DecimalFormat formatter = new DecimalFormat();
          sRC = formatter.format( sArg1 );
        } catch ( IllegalArgumentException e ) {
          throw Context.reportRuntimeError( "Could not apply the given format on the number : " + e.getMessage() );
        }
        break;
      case 2:
        try {
          if ( isNull( ArgList, new int[] { 0, 1 } ) ) {
            return null;
          } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {
            return (String) Context.getUndefinedValue();
          }
          double sArg1 = Context.toNumber( ArgList[0] );
          if ( Double.isNaN( sArg1 ) ) {
            throw Context.reportRuntimeError( "The first Argument must be a Number." );
          }

View on GitHub (pinned to f3058517a1)