Expert Consultancy from Yellow Pelican

Talend null Handling

A site about Talend

null Handling

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.

Literals, Primitive Type Variables & Class Instance Variables

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!"

Primitive Type Variables

Variables can represent data using Java Primitive Types.

Class Instance Variables

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;

Primitive Type Variable or Class Instance Variable?

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.

Handling null Class Instance Variables (Objects)

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).

Testing for null Pointers

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).

Example #1 - Bad

String myString = null;
if(myString.length() > 0) System.out.println(myString.toUpperCase());

Example #2 - Good

String myString = null;
if(myString != null && myString.length() > 0) System.out.println(myString.toUpperCase());

Other Considerations for String Instance Variables

Two other String values that you may want to give consideration to are: -

  • "" (a zero-length string)
  • "null" (the string "null")

Zero-length String

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.

"null" String (Talend Context)

When you create a new Context Variable, Talend assigns a Default value of null, as shown in the screenshot below.

Image 1

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.

Image 2

// tJava_1 Code

Image 3

Expert Consultancy from Yellow Pelican
comments powered by Disqus