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
7 REPLIES
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