A site about Talend
There's nothing more frustrating that having to look at 1000s of lines of Java code, to find the cause of a Null Pointer Exception (NullPointerException).
If you know what you're doing, it's usually not too much of a problem; A little understanding of Java Classes and Exceptions goes a long way to helping. This is as much a Java lesson than it is Talend.
A data item within your Talend Job may be stored as a Literal, a Primitive Type Variables or a Class Instance Variable, representing an Object of a particular Class.
Generally speaking, Literals are to be avoided; that is, a value has been hard-coded in to your source code. In the following code-fragment, "Hello World!" is a Literal.
String message = "Hello World!"
Variables can represent data using Java Primitive Types.
A Class Instance Variable is a Pointers to an instance of an Object, whose type is Class, for example, String.
In the following example, the Class Instance Variable mYString is a Pointers to an Object of the type String.
String mYString;
Certain data types may be stored as either Primitive Types or Objects. Have you ever noticed in a Talend Schema that integer data types, for example, are shown as int | Integer
. Why is this?
An integer can be stored as either a Primitive Type (int) or an Object (Integer). From an efficiency stand point, data is best stored as int, however, one important value that cannot be represented by int is null; if you assign no value, then the value of int is zero.
If you define a column as Nullable in your Schema, then an Integer type is used i.e. you need to be able to store a null value; otherwise, an int will be used.
When we're talking about null-handling in Talend (Java); we are specifically talking about handling Class Instance Variables (Objects) that are Null-pointers.
If you attempt to call a Method of Class Instance Variable that is a Null-pointers, Java will throw a Null Pointer Exception (NullPointerException).
It's simple to test for a Null-pointers, as shown below: -
if(myString == null) System.out.println("myString is null");
Talend provides the Routine (routines.Relational) public static boolean ISNULL(Object variable)
; which makes the same ==
test, as that shown above.
It is good practice to always test for a Null-pointers, before using an Object.
In these two, albeit unrealistic, examples, the first will throw a Null Pointer Exception (NullPointerException).
String myString = null; if(myString.length() > 0) System.out.println(myString.toUpperCase());
String myString = null; if(myString != null && myString.length() > 0) System.out.println(myString.toUpperCase());
Two other String values that you may want to give consideration to are: -
A zero-length String is something you will often want to handle, often converting it to a Null-pointer. Take an example where you are reading a CSV file and writing it to a MySQL Database.
When a string input from your CSV file is zero-length, it is sensible to store this as a NULL value in your database. The following Mapping Expression shows how you might handle this.
row1.MyString == null ? null : row1.MyString.length() == 0 ? null : row1.MyString;
You may, of course, want to wrap-up the above statement in to a Routine.
When you create a new Context Variable, Talend assigns a Default value of null
, as shown in the screenshot below.
At first glance, you may think that Talend is doing this to show that the Context Variable is, in fact, a Null-pointers; however, this is not the case. Talend has actually assigned "null"
as a value. If you remove this default value, the Context Variable will then become a zero-length String. To my mind, this is an unhelpful bug.
The other interesting observation with a Context Variable, is that when assigning values in the Contexts Tab, double-quoting is optional, as demonstrated below.
// tJava_1 Code System.out.println(context.new1); System.out.println(context.new2);