java.lang.NullPointerException

One Star

java.lang.NullPointerException

I have a simple job which uses Microsoft Excel spreadsheets as a source.  I need to take fields from different spreadsheets and make one output file.  I have a calculation in my tmap expression that uses multiple String fields that can be null in the source file.  When I run the job, I get the error java.lang.NullPointerException error.  I know I have to check for Null conditions upfront, but how do you do it for multiple fields?  All of my fields in the schema are set up to allow nulls.  
Below is what I have for my calculation in the expression of tmap with the null check for the fields that can be null at the beginning of the calculation.  All records return a Null on the output because at least one of the code fields is blank.  Instead of OR, I tried AND which gave me the NullPointerException error.   
UAFLossDraft.UAF_CODE1==null || UAFLossDraft.UAF_CODE2==null || UAFLossDraft.UAF_CODE3==null || UAFLossDraft.UAF_CODE4==null?null:UAFLossDraft.UAF_CODE1.equals("U")?"U":UAFLossDraft.UAF_CODE2.equals("H") || UAFLossDraft.UAF_CODE2.equals("E")?"U":MainDataFile.WARNING_CODES!=5 && UAFLossDraft.UAF_CODE1.equals("R") || UAFLossDraft.UAF_CODE2.equals("R") || UAFLossDraft.UAF_CODE3.equals("R") || UAFLossDraft.UAF_CODE4.equals("R")?"U":null 

I tried going into the Advanced settings of each Excel component and checked the Stop Reading on encountering empty 
I guess I could incorporate the null check within the calculation (I.E. UAFLossDraft.UAF_CODE1==null?null: UAFLossDraft.UAF_CODE1.equals("U")?"U": UAFLossDraft.UAF_CODE2==null?null...) but I can see that getting messy real quick as there are a couple of conditions that use different codes.
Is there any easier way to check for nulls on multiple fields used in calculations?
Sixteen Stars

Re: java.lang.NullPointerException

I'm not entirely sure what you want from your logic (brackets make it much easier to read and write combining multiple ANDs and ORs), but I think this might help. Write a String method (routine) to handle null values for you. Something like this (written freehand so might need checking)....
public static String removeNullString(String data){
return data == null ? "" : data;
}

That will return either the String data or an empty String.
You can then use it in your logic like so (I am assuming it is in a routine called TestRoutine).....
routines.TestRoutine.removeNullString(UAFLossDraft.UAF_CODE1).equals("U")?"U":routines.TestRoutine.removeNullString(UAFLossDraft.UAF_CODE2).equals("H") || routines.TestRoutine.removeNullString(UAFLossDraft.UAF_CODE2).equals("E")?"U":MainDataFile.WARNING_CODES!=5 && routines.TestRoutine.removeNullString(UAFLossDraft.UAF_CODE1).equals("R") || routines.TestRoutine.removeNullString(UAFLossDraft.UAF_CODE2).equals("R") || routines.TestRoutine.removeNullString(UAFLossDraft.UAF_CODE3).equals("R") || routines.TestRoutine.removeNullString(UAFLossDraft.UAF_CODE4).equals("R")?"U":null 

I have removed the initial check for all nulls because that will not happen now. A null will be returned as an empty String which will enable you to carry out the method calls without returning a NullPointerException.
As I said, this was written freehand so may need some checking and tweaking. But it should work for you.
One Star

Re: java.lang.NullPointerException

Thank you for your assistance.  I was able to get this to work and I am now getting the data I need without the NullPointerException error.  This really makes the calculation a lot cleaner and easier to read.
Thanks again!  I appreciate it.
One Star

Re: java.lang.NullPointerException

HI 
I wonder if there is some component in Talend to as the locale of the Java "https://docs.oracle.com/javase/tutorial/i18n/locale/create.html" or some conponente I can do code pure java ?
Seventeen Stars

Re: java.lang.NullPointerException

Set the locale of the JVM is static and affects the whole running vm. You can put this simple code with the help of the tJava component in your job.