{"record":{"id":"65ec1e87781bc401","repo":"pentaho/pentaho-kettle","slug":"error-executing-userdefinedjavaclass-getfields","errorCode":null,"errorMessage":"Error executing UserDefinedJavaClass.getFields(): ","messagePattern":"Error executing UserDefinedJavaClass\\.getFields\\(\\): ","errorType":"exception","errorClass":"KettleStepException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/userdefinedjavaclass/UserDefinedJavaClassMeta.java","lineNumber":489,"sourceCode":"    VariableSpace space, Repository repository, IMetaStore metaStore ) throws KettleStepException {\n    if ( !checkClassCookings( getLog() ) ) {\n      if ( cookErrors.size() > 0 ) {\n        throw new KettleStepException( \"Error initializing UserDefinedJavaClass to get fields: \", cookErrors\n          .get( 0 ) );\n      } else {\n        return;\n      }\n    }\n\n    try {\n      Method getFieldsMethod =\n        cookedTransformClass.getMethod(\n          \"getFields\", boolean.class, RowMetaInterface.class, String.class, RowMetaInterface[].class,\n          StepMeta.class, VariableSpace.class, List.class );\n      getFieldsMethod.invoke(\n        null, isClearingResultFields(), row, originStepname, info, nextStep, space, getFieldInfo() );\n    } catch ( Exception e ) {\n      throw new KettleStepException( \"Error executing UserDefinedJavaClass.getFields(): \", e );\n    }\n  }\n\n  public String getXML() {\n    StringBuilder retval = new StringBuilder( 300 );\n\n    retval.append( String.format( \"\\n    <%s>\", ElementNames.definitions.name() ) );\n    for ( UserDefinedJavaClassDef def : definitions ) {\n      retval.append( String.format( \"\\n        <%s>\", ElementNames.definition.name() ) );\n      retval.append( \"\\n        \" ).append(\n        XMLHandler.addTagValue( ElementNames.class_type.name(), def.getClassType().name() ) );\n      retval.append( \"\\n        \" ).append(\n        XMLHandler.addTagValue( ElementNames.class_name.name(), def.getClassName() ) );\n      retval.append( \"\\n        \" );\n      retval.append( XMLHandler.addTagValue( ElementNames.class_source.name(), def.getSource() ) );\n      retval.append( String.format( \"\\n        </%s>\", ElementNames.definition.name() ) );\n    }\n    retval.append( String.format( \"\\n    </%s>\", ElementNames.definitions.name() ) );","sourceCodeStart":471,"sourceCodeEnd":507,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/userdefinedjavaclass/UserDefinedJavaClassMeta.java#L471-L507","documentation":"Thrown from UserDefinedJavaClassMeta.getFields() when reflection invocation of the cooked class's static getFields(boolean, RowMetaInterface, String, RowMetaInterface[], StepMeta, VariableSpace, List) method fails. The step's generated class was cooked but its getFields implementation threw an exception, which is wrapped in a KettleStepException.","triggerScenarios":"During transformation init/field propagation, getFieldsMethod.invoke() throws because the user's code (or the generated class) raises an exception while computing output fields, or the method signature doesn't resolve.","commonSituations":"User code in the Class code tab throwing inside getFields (e.g. NPE on an unset variable); clearResultFields/field info lists containing nulls; version mismatch where generated getFields signature changed.","solutions":["Inspect the wrapped cause 'e' — the stack trace points at the failing line in your snippet's getFields logic.","Guard against nulls in your getFields code (variables, environment, row meta may be unset at init time).","Verify you don't modify output row structure based on runtime data not yet available at init.","Test the transformation with a sample dataset; field calculation happens before rows flow.","Avoid static state in your snippet that assumes processRow ran before getFields."],"exampleFix":"// before\nString v = getVariable(\"MY_VAR\");\nint n = Integer.parseInt(v);\n// after\nString v = getVariable(\"MY_VAR\", \"0\");\nint n;\ntry { n = Integer.parseInt(v); } catch (NumberFormatException e) { n = 0; }","handlingStrategy":"try-catch","validationCode":"// Ensure variables referenced in snippets have defaults\nif (getVariable(\"MY_VAR\", null) == null) throw new IllegalStateException(\"MY_VAR not set\");","typeGuard":null,"tryCatchPattern":"try {\n  trans.execute(null); // init path triggers getFields\n} catch (KettleStepException e) {\n  if (e.getMessage().contains(\"UserDefinedJavaClass.getFields()\")) {\n    e.getCause().printStackTrace(); // locate failing snippet line\n  }\n}","preventionTips":["Never assume runtime state in getFields code","Null-guard variables and environment in snippets","Preview the step during development to catch getFields exceptions early"],"tags":["kettle","udjc","reflection","step-init"],"backgroundTag":"internal-invariant-violation","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}