One Star

[resolved] Is there an easier way to handle nulls in tMap with lots of fields?

I've been working on a project where I have to map close to 100 fields. Everything was working perfectly, but I never got in the habit of testing for nulls. That came back to bite me because I was given a new file that had null values.
Currently the only way I know how to handle nulls are to use the following code in the expression editor:
FIELD != null && FIELD.length() > 0 ? FIELDVALUE : null
Since I have around 100 fields, editing every single expression to do this will take a lot of time. Is there an easier way?
5 REPLIES
One Star

Re: [resolved] Is there an easier way to handle nulls in tMap with lots of fields?

The question I'd have to ask is - what do you do when you hit nulls? Reject the record or route it for reprocessing? You can use the tSchemaComplianceCheck upstream to filter and segregate your records and you can use the codes in the error message to process the colums in a subsequent job - see the bottom of https://help.talend.com/search/all?query=tSchemaComplianceCheck&content-lang=en
Else you can use a tReplace... Or (much harder - I know you're looking for easy!), tJavaRow...
It might be a lot of work, once you do it, you'll sleep well knowing your job is resilient to null pointer errors...
One Star

Re: [resolved] Is there an easier way to handle nulls in tMap with lots of fields?

The question I'd have to ask is - what do you do when you hit nulls? Reject the record or route it for reprocessing? You can use the tSchemaComplianceCheck upstream to filter and segregate your records and you can use the codes in the error message to process the colums in a subsequent job - see the bottom of https://help.talend.com/search/all?query=tSchemaComplianceCheck&content-lang=en
Else you can use a tReplace... Or (much harder - I know you're looking for easy!), tJavaRow...
It might be a lot of work, once you do it, you'll sleep well knowing your job is resilient to null pointer errors...

If the field is empty I want it replaced by null, it doesn't matter if there are null values in the output, I just want the NullPointerException to go away. It seems weird to me that we have to test if the field is empty and manually set it to null, I figured Talend would see an empty field as null to begin with normally.
One Star

Re: [resolved] Is there an easier way to handle nulls in tMap with lots of fields?

Hi jeanjpotgieter - null pointer exceptions are among the most common Java errors. No need to defend Talend here, but because it generates java code, you have to deal with constraints of the coding language at certain points in your code, especially when you're processing and 'touching' your data.
There are tons of old posts on this forum on this error and they all have similar solutions to what I suggested; but you already know all of them and unfortunately, they don't provide that quick fix you're looking for...
One Star

Re: [resolved] Is there an easier way to handle nulls in tMap with lots of fields?

Hi jeanjpotgieter - null pointer exceptions are among the most common Java errors. No need to defend Talend here, but because it generates java code, you have to deal with constraints of the coding language at certain points in your code, especially when you're processing and 'touching' your data.
There are tons of old posts on this forum on this error and they all have similar solutions to what I suggested; but you already know all of them and unfortunately, they don't provide that quick fix you're looking for...

That's what I was afraid of. I guess I'll just spend the weekend cleaning out my files. At least I'll know to do this next time. Thanks for the help.
Seventeen Stars

Re: [resolved] Is there an easier way to handle nulls in tMap with lots of fields?

hi all,
as willm as said it's our job to manage data & null value Smiley Wink
Is it normal that you've got so much empty or null value ? Profile your data sources to check percentage of null values if necessary ...
but never mind , it 's not your need at that case I guess Smiley Happy
tSchemacComplianceCheck has en advanced options that can "convert" empty fields in null value.

hope it helps
regards
laurent