tXMLMAP: expressions and java.lang.NullPointerException

One Star

tXMLMAP: expressions and java.lang.NullPointerException

Hello,
I have the following expression in tXMLMAP component.
Although I check every variable before to use it in the calcul, I have a java.lang.NullPointerException 
What is wrong with my expression?
Thanks in advance.
!= null && != null && != null && !=null && .equals("POD")? 
(Float.valueOf()+Float.valueOf()-Float.valueOf() )/1000 :
!= null && != null && !=null && .equals("JSF")? 
(Float.valueOf()+Float.valueOf())/1000 : null 
One Star

Re: tXMLMAP: expressions and java.lang.NullPointerException

This part works alone:
!= null && != null && != null && !=null && .equals("POD")? 
(Float.valueOf()+Float.valueOf()-Float.valueOf() )/1000 : null
And this works alone:
!= null && != null && !=null && .equals("JSF")? 
(Float.valueOf()+Float.valueOf())/1000 : null 
But together it doesn't work.
I want to do something like:
if condition1 then A1
elseif condition 2 A2
else A3
Employee

Re: tXMLMAP: expressions and java.lang.NullPointerException

Can you send the XML that is causing it to fail? 
One Star

Re: tXMLMAP: expressions and java.lang.NullPointerException

Hello,
Attatched you find the XML that is causing it to fail.
test.txt_20150518-1055.txt
Employee

Re: tXMLMAP: expressions and java.lang.NullPointerException

There don't seem to be any problems with that. It is kind of hard to debug an issue like this without running the job and then inspecting the code. Have you looked at the line of generated code this falls over on? In the error message it says line 2965. What does that line do? When you say it works when you separate the code into two, how do you use the result? Are both null?
One Star

Re: tXMLMAP: expressions and java.lang.NullPointerException

Hi,
Actually the problem is at the line 2567, attached you find the part of the source code including that line.
when I seperate the code, I mean that I calculate the variable DDPT only for "POD" or "JSF", so for the other one it is null.
Employee

Re: tXMLMAP: expressions and java.lang.NullPointerException

Hmm, have you made changes to your XML or to the Job? The error in your previous error message was elsewhere. I have a suggestion to help you track this. Go to your tXMLMap and set it up to allow rejects to be returned. See the image below...
In the top left, click on the button that is circled and then untick the "Die on Error" tick box. This will provide a "Rejects" output from the tXMLMap. YOu can hook that up to a tLogRow.
Next, create another output from your tXMLMap and pass every data column that you are using from your XML to that output without changing them. 
Run the job and take a look at the data and the error messages. It should provide a bit more info on what is going wrong.
One Star

Re: tXMLMAP: expressions and java.lang.NullPointerException

I changed the order of my output variables that's way the error changed the place but it's the same error and concerns the same expression.
I can't find the circled button, see the attached print screen.
I attatched also the job so that you can import it and see what's wrong.
Thanks in advance.
collecte_fronataux.zip.zip
Employee

Re: tXMLMAP: expressions and java.lang.NullPointerException

My mistake, I was talking about the tMap component. It's useful to remember though.....(and should be implemented in the tXMLMap component).
Don't change the order of your outputs, just create another output table and add every data column to it without ANY data manipulation. No formulas, no expressions, nothing. Just the untouched data. Run that and see what is produced. Something is returning null (or maybe an empty string) where you are not expecting it.
One Star

Re: tXMLMAP: expressions and java.lang.NullPointerException

I added a new output tLogRow with the columns used to calculate the variable DDPT.
We see that only for the second line of the file that we have values for all columns.
I deleted all lines except the second line, the job is OK.
But in the expression of DDPT I check every variable, if at least one of the variables used in the expression is null the variable DDPT must be null.
For me the null variables are not used in the expression, because if it's null => DDPT=null the rest of the expression with class methods is not executed.
What's wrong with my expression?
Employee

Re: tXMLMAP: expressions and java.lang.NullPointerException

OK, this is good news. I can't see all of the data types in your screen shot, but I can see enough to assume that maybe the datatype for the fields that "!=null" doesn't work for are maybe "String". To get round this, try something like .....
.trim().compareToIgnoreCase("")!=0 
.....as well as !=null
If it is this, then maybe it might be a bit neater to write a Routine to do this checking for you. So you could have a Routine with a method called "checkNotEmpty(String column)" (or similar) and use that to tidy up your expressions.
One Star

Re: tXMLMAP: expressions and java.lang.NullPointerException

With the solution that you suggest I have always an error.
The variables used in the expression are coming diredtly from the XML file:
all_tmp.DDPT = treeNodeAPI_tXMLMap_1_TXMLMAP_OUT
.get_String(
"row1.METRO:/METRO/METROLOGIE/T2_T0")
.trim().compareToIgnoreCase("") != 0
&& treeNodeAPI_tXMLMap_1_TXMLMAP_OUT
.get_String(
"row1.METRO:/METRO/METROLOGIE/T1_T3")
.trim()
.compareToIgnoreCase("") != 0
Employee

Re: tXMLMAP: expressions and java.lang.NullPointerException

Hmmmm I think you *may* have stumbled upon a *feature* :-) 
I decided to rebuild your job using the XML you supplied to see what was happening and found a way around this. I couldn't see anything wrong with your expression, so re-worked it using the tXMLMap variables. I created two Float variables (making sure that the Nullable box was ticked). The expression for the first variable (Var.Var1) was set to .....

!= null && != null && != null && !=null && .equals("POD")? 
(Float.valueOf()+Float.valueOf()-Float.valueOf() )/1000 : null


The expression for the second variable (Var.Var2) was set to .....
!= null && != null && !=null && .equals("JSF")? 
(Float.valueOf()+Float.valueOf())/1000 : null 

I then used the variables in the expression for the output value like below...
Var.Var1!=null ? Var.Var1 : Var.Var2!=null ? Var.Var2 : null 

This worked. I would raise this issue with Talend support if you have the Enterprise Edition
One Star

Re: tXMLMAP: expressions and java.lang.NullPointerException

Hello,
No, we don't have the Enterprise Edition but you can raise it as bug, no?
I tried to do it like what you described above, it couldn't find var1 and var2.
what's wrong?
Employee

Re: tXMLMAP: expressions and java.lang.NullPointerException

You need to create the Var1 and Var2 variables. They were tMap variables that I created for the example. The variables section is between the input and output sections of the tMap config display.
One Star

Re: tXMLMAP: expressions and java.lang.NullPointerException

I created var1 and var2 in the tXMLMAP iteself and used them in the same component, is that possible?
Or I have to add a new tMAP after tXMLMAp to calculate the variable var based on var1 and var2?
Employee

Re: tXMLMAP: expressions and java.lang.NullPointerException

Yes, that is right. When I said tMap, I meant tXMLMap. Create the variables in the tXMLMap and assign them values using the expressions described in my solution. Then use .....
Var.Var1!=null ? Var.Var1 : Var.Var2!=null ? Var.Var2 : null 

... in your output column.
One Star

Re: tXMLMAP: expressions and java.lang.NullPointerException

Thanks it works!
But I think that it is not normal to be unable to make expressions with if elseif else in the tXMLMAP directly.
we have to make them in two steps!
It deserves to raise the issue to talend developpement team.
Employee

Re: tXMLMAP: expressions and java.lang.NullPointerException

I agree. I have only seen this issue in your example though. I use inline IFs a lot in my tMap/tXMLMap components. I suspect that it is something to do with processing an XML  entity in a single expression more than once. It looks like the first time the entity is checked (or used) it is fine, but the second time it is null.
I should probably point out that I no longer work for Talend and have my own business working with Talend tools. My user on here still shows as a "Talend User" and I am looking to get that changed. If I find the cause of this issue I will pass it on to the guys at Talend.