A site about Talend
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.
String message = "Hello World!"
Variables can represent data using Java Primitive Types.
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.
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.
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: -
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);
comments powered by Disqus