tmap error converting null to Double

One Star

tmap error converting null to Double

I am reading an Excel file and have the following values in a field .... null, "1234.00", "1234.00-". I am putting the "-" in front of the field and converting it to a Double but when it finds a "null", I get an exception error. How do I handle the "null" value?
Exception Error:
Double.parseDouble(Unknown Source)
tMap Code into a Var:
StringHandling.INDEX(row1.Field,"-")>0?Double.parseDouble("-"+StringHandling.TRIM(StringHandling.EREPLACE(row1.Field,"-"," ")))Smiley Very Happyouble.parseDouble(StringHandling.TRIM(row1.Field))

Thanks
One Star

Re: tmap error converting null to Double

Hi,
you a should a test to check if the variable is null
Relational.ISNULL(row1.Field) ? null : (StringHandling.INDEX(row1.Field,"-")>0?Double.parseDouble("-"+StringHandling.TRIM(StringHandling.EREPLACE(row1.Field,"-"," "))):Double.parseDouble(StringHandling.TRIM(row1.Field))  )
One Star

Re: tmap error converting null to Double

Now the exception message has changed to
Exception in component tMap_1
java.lang.NullPointerException
on the Debug Trace, the field shows .... null from an Excel spreadsheet. I even tried comparing to the field to "null" in case it came in as a string.

Relational.ISNULL(row1.ESCMP__1)|| (row1.ESCMP__1=="null")? null : (StringHandling.INDEX(row1.ESCMP__1,"-")>0?Double.parseDouble("-"+StringHandling.TRIM(StringHandling.EREPLACE(row1.ESCMP__1,"-"," ")))Smiley Very Happyouble.parseDouble(StringHandling.TRIM(row1.ESCMP__1)))
One Star

Re: tmap error converting null to Double

you can try that:
Relational.ISNULL(row1.ESCMP__1)|| (StringHandling.TRIM(row1.ESCMP__1)=="")? null : (StringHandling.INDEX(row1.ESCMP__1,"-")>0?Double.parseDouble("-"+StringHandling.TRIM(StringHandling.EREPLACE(row1.ESCMP__1,"-"," "))):Double.parseDouble(StringHandling.TRIM(row1.ESCMP__1)))

maybe your field is not nullable
can you put 0 instead of null
Relational.ISNULL(row1.ESCMP__1) ? new Double(0) : (StringHandling.INDEX(row1.ESCMP__1,"-")>0?Double.parseDouble("-"+StringHandling.TRIM(StringHandling.EREPLACE(row1.ESCMP__1,"-"," "))):Double.parseDouble(StringHandling.TRIM(row1.ESCMP__1)))
One Star

Re: tmap error converting null to Double

that worked, thanks very much, been working on it for more than I want to admit !
Community Manager

Re: tmap error converting null to Double

123
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: tmap error converting null to Double

You're welcome ! it often goes like that ...
Which solution worked?
One Star

Re: tmap error converting null to Double

Putting in the new Double(0) instead of null on the true test.
Relational.ISNULL(row1.field)|| (row1.field=="null")? new Double(0): (StringHandling.INDEX(row1.field,"-")>0?Double.parseDouble("-"+StringHandling.TRIM(StringHandling.EREPLACE(row1.field,"-"," ")))Smiley Very Happyouble.parseDouble(StringHandling.TRIM(row1.field)))

value result
null 0
"1234.56" 1234.56
"1234.56-" -1234.56

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

Definitive Guide to Data Integration

Practical steps to developing your data integration strategy.

Download