One Star

tMap NullPointerException - missing row

I think this is a really easy one, only I seem to lack that piece of knowledge.
I'm performing a Left Join with data from two tables on one simple id.
The main flow table sometimes lack a row/id which exists in the lookup table.
When the job gets to that row/id the tMap throws a
Exception in component tMap_3
java.lang.NullPointerException
How do I avoid this?
7 REPLIES
Seventeen Stars

Re: tMap NullPointerException - missing row

The main reason for NullPointerException in the tMap is the so called Unboxing of primitive data type wrapper objects.
This means: your read a column which is nullable and write into a column which is not nullable. Especially for numeric data types this leads to this problem.
Lets assume you read an "Integer" (refer to the schema column and check if nullable) and write into a "int" (not nullable).
If you map this the compiler generates this code: row1.my_integer.intValue()
If row1.my_integer is null -> Bang we have the mentioned NullPointerException.
Unfortunately you can only find the causing column if you take a look into the source code (refer to the stack trace of the exception).
To avoid that write this a expression in the output flow:
row1.my_integer != null ? row1.my_integer : 0
This checks the null state and if it is null, it sends a zero to the output flow.
One Star

Re: tMap NullPointerException - missing row

Thanks!
This row1.my_integer != null ? row1.my_integer : 0
in the output flow.
/Patrick
One Star

Re: tMap NullPointerException - missing row

Hi Jlolling
If my target column is Nullable and I want to assign a Null value then what should I do. It throws a null pointer exception even if I am getting a null from the source and my target allows Nulls.
I am writing into HDFS and my column is of BigDecimal data type
Seventeen Stars

Re: tMap NullPointerException - missing row

Hard to say. It looks like your target column is not nullable.
Could you switch to the Code view and go to the line mentioned in the Exception stack trace.
Here you will find a Java assigment and this code post here (please show us the line line number where it happens).
One Star

Re: tMap NullPointerException - missing row

Hi,
The following is the code snippet from the code viewer:
sb_tHDFSOutput_13.append(row21.surplus_factor.toPlainString());
The column surplus_factor mentioned above is of type BigDecimal and is a nullable columns in the tHDFSOutput. Nulls are coming in from the source and it is trying to do an operation as mentioned above and hence failing. How can I specify a default value of NULL in HDFS in this case.
Seventeen Stars

Re: tMap NullPointerException - missing row

Hello,
the problem is, a default value does not work here because the default values in the schema are only in use for creating tables statements.
I guess the mentioned source code is in the tHDFSOutput component and therefore you cannot change it.
In this vase try add a tMap in front of the tHDFSOutput and change the datatype in the output flow to a simple data type like double and fill this output column in the tMap with a expression like this:
row21.surplus_factor != null ? row21.surplus_factor.doubleValue() : 0

the new output now gets never a null value and therefore should not fail.
Good luck.
Four Stars

Re: tMap NullPointerException - missing row

The main reason for NullPointerException in the tMap is the so called Unboxing of primitive data type wrapper objects.
This means: your read a column which is nullable and write into a column which is not nullable. Especially for numeric data types this leads to this problem.
Lets assume you read an "Integer" (refer to the schema column and check if nullable) and write into a "int" (not nullable).
If you map this the compiler generates this code: row1.my_integer.intValue()
If row1.my_integer is null -> Bang we have the mentioned NullPointerException.
Unfortunately you can only find the causing column if you take a look into the source code (refer to the stack trace of the exception).
To avoid that write this a expression in the output flow:
row1.my_integer != null ? row1.my_integer : 0
This checks the null state and if it is null, it sends a zero to the output flow.

Hello Jlolling.
How can we know which column is causing the this exception . i have all source and target column null able.  how we find in stack trace which column is throwing this error as it only shows what column values are getting transported while there is error. even when i go to code i dont see any red mark as we see when there is syntax error. appreciate your help.