Talend Open Studio and NullPointerException in tMap

The tMap component in Talend Open Studio is a very powerfull component, however is common to encounter an exception message like this:

Exception in component tMap_1 java.lang.NullPointerException
at test.test_0_1.Test.tMysqlInput_1Process(Test.java:2502)
at test.test_0_1.Test.runJobInTOS(Test.java:2793)
at test.test_0_1.Test.main(Test.java:2705)

The exception message is shown in the picture:

Talend java.lang.NullPointerException in component tMap_1

Let's have a look at the tMap component definition:

Talend NullPointerException component tMap

row1.birthdate.getYear() calls a java method on row1.birthdate, however if row1.birthdate is null (for example the record field is null) Talend produces a NullPointerException. To fix this error we need to replace the original expression

row1.birthdate.getYear()

with a Java expression that accepts null values, for example using a ternary operator (if-then-else):

(row1.birthdate != null ) ? row1.birthdate.getYear() : null

The fields of tMap component can handle Java expression but not code like if, else if, else because they are control flow operators. An expression is a piece of code in Java that produces a result and can be used as paramether for a function. This is because Talend translates the tMap expressions in Java code.

To implement the new code just use the Expression Builder dialog, as in the following picture:

Talend NullPointerException component tMap fix

Most of Talend components can take Java expressions as parameters. It's always a good idea to check if object is not null expecially in Java, where a method call on objects that are null (for example a null field) produces a NullPointerException.

tags: