[resolved] Null pointer exception - tMap

One Star

[resolved] Null pointer exception - tMap

Hi everyone,
I am facing this error (c.f screenshot) for a while now. Usually Google is my friend, but not today.
Thank you for your advice,

EDIT :
I think the error comes from the absence of a null test. Therefore I tried this 3 kind of test but none works.

(String)row7.selling_price.equals(null))?0:row7.selling_price

(row7.selling_price.toString().equals(null))?0:row7.selling_price

(row7.selling_price == null)?0:row7.selling_price
Moderator

Re: [resolved] Null pointer exception - tMap

Hi,
There is null pointer exception in your tMap component.
You need to handle the null value. Is your row7.selling_priceP"Integer" data type? If so, please parse Int to String firstly and then
row7.selling_priceP)==null||row7.selling_priceP.isEmpty())? value if it is null:do your further processing
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
One Star

Re: [resolved] Null pointer exception - tMap

Hi xdshi,
What do you mean by "parse Int to String"
How can I do that and where ?
Thx,
Moderator

Re: [resolved] Null pointer exception - tMap

Hi,
Please take a look at the attached screenshots.
Or you can use String.valueof in your expression, such as, String.valueOf(row1.newColumn)==null||(String.valueOf(row1.newColumn).isEmpty()
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
One Star

Re: [resolved] Null pointer exception - tMap

The following code doesn't work (cf. screenshot) :/
(String.valueOf(row8.payment_date)==null||String.valueOf(row8.payment_date).isEmpty())?false:true
I mean I get a "true" while I should get a "false" since row8.payment_date = null
Moderator

Re: [resolved] Null pointer exception - tMap

Hi,
The ternary operation in tMap should be:
Condition?value if true: value if false
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
One Star

Re: [resolved] Null pointer exception - tMap


My code is respecting the format.

My destination_field is a boolean.

(String.valueOf(row8.payment_date)==null||String.valueOf(row8.payment_date).isEmpty())?false:true     

IF row8.payment_date is null or empty
   destination_field = false
ELSE
   destination_field = true



I face the same issue with a destination_field as a String and this code.


(String.valueOf(row8.payment_date)==null||String.valueOf(row8.payment_date).isEmpty())  ? "NULL OR EMPTY" : "NOT NULL AND NOT EMPTY"




I also tried to use the following code but it doesn't work neither.


(Relational.ISNULL(row8.payment_date)?false:true
Moderator

Re: [resolved] Null pointer exception - tMap

Hi,
What's the actual result you are facing? Is it still null pointer exception in tMap? Have you tried to check "Code" tab to see if there is any compile error?
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
One Star

Re: [resolved] Null pointer exception - tMap

I am still looking for the right code to use in order to check if my Date field is null or not.
As you can see on the screenshot below this code doesn't give me the right output value.
Since the value of payment_date=null, that means that the condition is true then I should get the value right after the '?' which is in my case false.
We can clearly see on the screenshot I get a true Smiley Sad
(String.valueOf(row8.payment_date)==null||String.valueOf(row8.payment_date).isEmpty())?false:true

Thx a lot for helping me on that issue Smiley Happy
Sixteen Stars

Re: [resolved] Null pointer exception - tMap

Why are you using "String.valueOf"? It is that which is causing you problems. To check to see if an object is null, all you need to do is use "==null". You also won't get an empty date if the object is a Date. So your check should be similar to below I believe....
row8.payment_date==null ? false:true
One Star

Re: [resolved] Null pointer exception - tMap

Hi rhall_2.0,
I tried your solution before and as you can see on the screenshot below, it doesn't work neither :/
Thx anyway.
One Star

Re: [resolved] Null pointer exception - tMap

Just to summarize, please find below all the codes I already tried and that lead either to an ERROR or either to the wrong VALUE
WRONG VALUE Relational.ISNULL(row8.payment_date)?false:true
WRONG VALUE row8.payment_date==null?false:true
WRONG VALUE row8.payment_date.equals(null)?false:true
ERROR row8.payment_date.isEmpty()?false:true
ERROR row8.payment_date==""?false:true
WRONG VALUE String.valueOf(row8.payment_date)==null?false:true
WRONG VALUE String.valueOf(row8.payment_date).equals(null)?false:true
WRONG VALUE String.valueOf(row8.payment_date).isEmpty()?false:true
WRONG VALUE String.valueOf(row8.payment_date)==""?false:true
WRONG VALUE row8.payment_date.toString()==null?false:true
WRONG VALUE row8.payment_date.toString().equals(null)?false:true
WRONG VALUE row8.payment_date.toString().isEmpty()?false:true
ERROR row8.payment_date.toString()=""?false:true
ERROR (String)row8.payment_date==null?false:true
ERROR (String)row8.payment_date.equals(null)?false:true
ERROR (String)row8.payment_date.isEmpty()?false:true
ERROR (String)row8.payment_date==""?false:true
Thank you for your advice,
One Star

Re: [resolved] Null pointer exception - tMap

Hi,
Have you already tried like this?
(row8.payment_date.equals(""))?false:true 
Regards, May T.
One Star

Re: [resolved] Null pointer exception - tMap

Hi May T,
Thank you for this solution I didn't think about, but still it's not working (screenshot below)

One Star

Re: [resolved] Null pointer exception - tMap

Hi,
Don't test with "Test" button of Expression Builder.
Can you please test by running the whole Job (with that code).
Regards, May T.
One Star

Re: [resolved] Null pointer exception - tMap

I finally find something working, but it's dirty Smiley Happy
I don't get the null pointer exception error anymore.
((String.valueOf(row8.payment_date).equals("null"))
||
(String.valueOf(row8.payment_date).equals("NULL"))
||
(String.valueOf(row8.payment_date).isEmpty()))
?false
:true 


Thanks to all of you,
One Star

Re: [resolved] Null pointer exception - tMap

May T, I tried to run the job with your code, but I got the null pointer exception again.
Since I am facing so many troubles, do you think I forgot to configure something in Talend?
Thx.
One Star

Re: [resolved] Null pointer exception - tMap

Hi,
I think it's not related with configuration.
I'm not sure but I think it's depend on Type(data type) of your components and data file.
Regards, May.
One Star

Re: [resolved] Null pointer exception - tMap


In MySQL, the field is recorded as DATE (with no parameters) and the values look like 'YYYY-MM-DD'
In Talend, inside the tMap, the field is identified as Date "dd-MM-yyyy"
=> So, I change the format in the tMap to "yyyy-MM-dd", but it doesn't change anything. I still got the error with the code below.

(row8.payment_date.equals(""))?false:true 

Anyway, thank you for your help, it seems that I got no choice to keep my dirty solution Smiley Happy
Sixteen Stars

Re: [resolved] Null pointer exception - tMap

Is "payment_date" a Date or a String? I assumed it was a Date since I didn't see anywhere where it said otherwise. If it is a String, then it is entirely possible that null is being represented as a String of "null". This is an annoying "feature" of Talend with the help of Java. If you have a null String and you concatenate it with a populated String, the null will be converted to "null".
Demonstrated in the example below.....
String hello = "hello";
String nullValue = null;
String result = hello+nullValue;
System.out.println(result);
If you run that code you will get ....
"hellonull"
You need to be aware of this when working with Strings that may have gone through a similar process.
One Star

Re: [resolved] Null pointer exception - tMap

Thx for the information rhall_2.0.
In the MySQL DB, payment_date has the type DATE and the values look like "2015-11-13".
So far, I can't find a better solution than this one:
((String.valueOf(row8.payment_date).equals("null"))
||
(String.valueOf(row8.payment_date).equals("NULL"))
||
(String.valueOf(row8.payment_date).isEmpty()))
?false
:true 
Sixteen Stars

Re: [resolved] Null pointer exception - tMap

OK, I think I understand what is wrong. If payment_date is definitely a Date the below code will work....
row8.payment_date==null ? false:true

 The Expression tester is useless and I am guessing you are basing your tests on that. I just put together the below test job to prove this....

The tFixedFlowInpt config is below....


The tMap config is below....


The Expression with the test result is below.....


What is actually produced by the tLogRow is below....


Basically, the Test functionality is flawed and you should try and test things by printing out to the console. It shouldn't be this way, but it is just one of those things you learn when using Talend.
One Star

Re: [resolved] Null pointer exception - tMap

Thank you a lot rhall_2.0!
I loose so much time on that Smiley Happy
Indeed, it is just a bug at the test level.
Thanks to everyone.