Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Discover how organizations are unlocking new revenue streams: Watch here
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

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,"-"," "))) 0683p000009MACJ.pngouble.parseDouble(StringHandling.TRIM(row1.Field))

Thanks
Labels (2)
7 Replies
ytuzet
Creator
Creator

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))  )
Anonymous
Not applicable
Author

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,"-"," "))) 0683p000009MACJ.pngouble.parseDouble(StringHandling.TRIM(row1.ESCMP__1)))
ytuzet
Creator
Creator

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)))
Anonymous
Not applicable
Author

that worked, thanks very much, been working on it for more than I want to admit !
Anonymous
Not applicable
Author

123
ytuzet
Creator
Creator

You're welcome ! it often goes like that ...
Which solution worked?
Anonymous
Not applicable
Author

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,"-"," "))) 0683p000009MACJ.pngouble.parseDouble(StringHandling.TRIM(row1.field)))

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