I am using talend for some ETL operations, I was trying to use ternary operator for some logical work but getting nullpointer
exception from talend.
What I want to do - I am having source database which is having one column in table named "time_zone" its of integer type, containing Enum like- 1,2,3,4.....12 in destination table I want to replace these with some String like is the value is 0 I want to write "EST_GMT_0" in my destination table.
What I tried - Earlier I tried one approach that worked but now I found some issues on that approach -
String.valueOf(row12.time_zone).replace("0","AST_GMT_4").replace("1","EST_GMT_5").replace("2","CST_GMT_6").replace("3","MST_GMT_7").replace("4","PST_GMT_8").replace("5","AST_GMT_9").replace("6","HST_GMT_10").replace("7","ADT_GMT_3").replace("8","EDT_GMT_4").replace("9","CDT_GMT_5").replace("10","ADT_GMT_6").replace("11","ADT_GMT_7").replace("12","ADT_GMT_8")
Earlier I was doing it like this but the issue here is the output will be not as expected It also replaces the last replaced String, because replaced string containing number like EDT_GMT_4
replace method again replaces 4 with some other String value and soo on until it wont react higher values, as in the result the final output becomes - MST_GMT_ADT_GMT_MST_GMT_7
like this
Other Approach I tried -
String.valueOf(row12.time_zone).equals("0")?String.valueOf(row12.time_zone).replace("0","ZERO"):String.valueOf(row12.time_zone).equals("1")?String.valueOf(row12.time_zone).replace("1","One"):String.valueOf(row12.time_zone).equals("2")?String.valueOf(row12.time_zone).replace("2","Two"):String.valueOf(row12.time_zone).equals("3")?String.valueOf(row12.time_zone).replace("3","Three"):String.valueOf(row12.time_zone).equals("4")?String.valueOf(row12.time_zone).replace("4","Four"):String.valueOf(row12.time_zone).equals("5")?String.valueOf(row12.time_zone).replace("5","Five"):String.valueOf(row12.time_zone).equals("6")?String.valueOf(row12.time_zone).replace("6","Six"):String.valueOf(row12.time_zone).equals("7")?String.valueOf(row12.time_zone).replace("7","Seven"):String.valueOf(row12.time_zone).equals("8")?String.valueOf(row12.time_zone).replace("8","Eight"):String.valueOf(row12.time_zone).equals("9")?String.valueOf(row12.time_zone).replace("9","Nine"):row12.time_zone.equals("10")?String.valueOf(row12.time_zone).replace("10","Ten"):String.valueOf(row12.time_zone).equals("11")?String.valueOf(row12.time_zone).replace("11","Eleven"):String.valueOf(row12.time_zone).equals("12")?String.valueOf(row12.time_zone).replace("12","Twelve"):String.valueOf(row12.time_zone)
That was the second approach I tried but got NullPointerException
How to resolve this ?
In your other approach, for the value ten (10) replacement, you forgot to apply the same java method String.valueOf() to convert integer to String before you can go through comparison tests. Some of your input rows (integer type) are null, and you cannot apply directly string equals method to those rows.
So, that part of your ternary operation
:row12.time_zone.equals("10")?String.valueOf(row12.time_zone).replace("10","Ten")
should be this (like the other lines)
:String.valueOf(row12.time_zone).equals("10")?String.valueOf(row12.time_zone).replace("10","Ten")