[resolved] Assign null to float results in NullPointerException

One Star

[resolved] Assign null to float results in NullPointerException

Hi,
I'm trying to fill the parameters for an Oracle stored procedure. Some of the parameters (4) are of type Float.
In three cases I assign null to the parameter directly in the tMap (see screen shot)
In the other case I try to determine if one of my input row fields (a float as well) equals null. Is it doesn't equal to null, I use the floatValue() method to put a float value in the column and in the other case I want to assign null to the output column. This is where a strange nullpointer exception shows up.
The code I use to fill the column is as follows:
(row1.AIM_PURCHASE_PRICE != null) ? row1.AIM_PURCHASE_PRICE
.floatValue()
: null

The error appears at the third line, exactly where null is assigned to the variable, so it's not that my input row isn't correct.
Any one knows where this error is coming from and how I can fix this?

Accepted Solutions
One Star

Re: [resolved] Assign null to float results in NullPointerException

In addition:
I found that when I slightly change the code it seems to be working okay.
migrate_manual_offer_entries_tmp.p_start_date = row1.AIM_START_DATE;
migrate_manual_offer_entries_tmp.p_end_date = row1.AIM_END_DATE;
migrate_manual_offer_entries_tmp.p_net_pur_price_in_eur = (row1.AIM_PURCHASE_PRICE != null) ? row1.AIM_PURCHASE_PRICE
.floatValue()
: null;
migrate_manual_offer_entries_tmp.p_transp_cost_in_eur = null;
migrate_manual_offer_entries_tmp.p_cap_in_eur = null;
migrate_manual_offer_entries_tmp.p_vat_pct = null;

The question that remains is: WHY?
The code in the previous post doesn't have any syntax errors as far as I can see. So what's wrong with that?
Hope any java-guru can shine a light on this for me.
Regards,
Arno
One Star

Re: [resolved] Assign null to float results in NullPointerException

Hi,
It looks like I had my own brilliant idea.... In other words: I haven't been so brilliant the last couple of days and now saw where I went wrong.
I forgot to check the "nullable" checkbox in the variables in my tMap, therefor the variables were set to float in stead of Float. Once checked, they changed to Float and my code magically started to work as expected Smiley Wink
Still not sure why the code refused to work without the variables, like in the code shown above, but my job runs for now and that's what counts!
Thanks for your help Shong!
Best regards,
Arno

All Replies
Community Manager

Re: [resolved] Assign null to float results in NullPointerException

Hi
I don't think it will throws NPE if you assign null to the parameter directly on tMap, Do you have other output table?
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] Assign null to float results in NullPointerException

Hi Shong,
Thanks for your reply!
Yes there are other output tables, but the error is specific on this line of code.
I (java-)debugged the code and found it to throw the error on the line where null is assigned to the column.
Other column get a null assigned directly, as you can see in the code below.
								migrate_manual_offer_entries_tmp.p_start_date = row1.AIM_START_DATE;
migrate_manual_offer_entries_tmp.p_end_date = row1.AIM_END_DATE;
migrate_manual_offer_entries_tmp.p_net_pur_price_in_eur = (row1.AIM_PURCHASE_PRICE != null) ? row1.AIM_PURCHASE_PRICE
.floatValue()
: (row1.PDS_PURCHASE_PRICE != null) ? row1.PDS_PURCHASE_PRICE
.floatValue()
: null;
migrate_manual_offer_entries_tmp.p_transp_cost_in_eur = null;
migrate_manual_offer_entries_tmp.p_cap_in_eur = null;
migrate_manual_offer_entries_tmp.p_vat_pct = null;

These last three assignments don't throw any error. It's just the line before.
Any thoughts on why this happens?
Regards,
Arno
One Star

Re: [resolved] Assign null to float results in NullPointerException

In addition:
I found that when I slightly change the code it seems to be working okay.
migrate_manual_offer_entries_tmp.p_start_date = row1.AIM_START_DATE;
migrate_manual_offer_entries_tmp.p_end_date = row1.AIM_END_DATE;
migrate_manual_offer_entries_tmp.p_net_pur_price_in_eur = (row1.AIM_PURCHASE_PRICE != null) ? row1.AIM_PURCHASE_PRICE
.floatValue()
: null;
migrate_manual_offer_entries_tmp.p_transp_cost_in_eur = null;
migrate_manual_offer_entries_tmp.p_cap_in_eur = null;
migrate_manual_offer_entries_tmp.p_vat_pct = null;

The question that remains is: WHY?
The code in the previous post doesn't have any syntax errors as far as I can see. So what's wrong with that?
Hope any java-guru can shine a light on this for me.
Regards,
Arno
Community Manager

Re: [resolved] Assign null to float results in NullPointerException

Hi
migrate_manual_offer_entries_tmp.p_net_pur_price_in_eur = (row1.AIM_PURCHASE_PRICE != null) ? row1.AIM_PURCHASE_PRICE
.floatValue()
: (row1.PDS_PURCHASE_PRICE != null) ? row1.PDS_PURCHASE_PRICE
.floatValue()
: null;

Try to add () enclose the right side of :, change the code to:
migrate_manual_offer_entries_tmp.p_net_pur_price_in_eur = (row1.AIM_PURCHASE_PRICE != null) ? row1.AIM_PURCHASE_PRICE
.floatValue()
: ((row1.PDS_PURCHASE_PRICE != null) ? row1.PDS_PURCHASE_PRICE
.floatValue()
: null);
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] Assign null to float results in NullPointerException

Hi Shong,
I tried that, but unfortunately it didn't work. It still fail on the same line of code.
It appeared to me that when I try to "split" the problem by creating two variables (in the middle of my tMap), and I make these variables of type Float, Talend changes them to be float. As float is a different type than Float I could understand the problem, because the object instance Float can be null (i.e. not assigned an object) where float should always be a number and therefor cannot contain null.
Question I have now is: why doesn't Talend allow me to have a Float type variable?
Hope anyone has a brilliant idea, I could really use that Smiley Happy
Regards,
Arno
One Star

Re: [resolved] Assign null to float results in NullPointerException

Hi,
It looks like I had my own brilliant idea.... In other words: I haven't been so brilliant the last couple of days and now saw where I went wrong.
I forgot to check the "nullable" checkbox in the variables in my tMap, therefor the variables were set to float in stead of Float. Once checked, they changed to Float and my code magically started to work as expected Smiley Wink
Still not sure why the code refused to work without the variables, like in the code shown above, but my job runs for now and that's what counts!
Thanks for your help Shong!
Best regards,
Arno
Community Manager

Re: [resolved] Assign null to float results in NullPointerException

Hi Arno
Glad to hear that!Smiley Wink Thanks for your feedback!
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business