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
The exception message is shown in the picture:
Let's have a look at the tMap component definition:
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
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:
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.