Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[resolved] What is a NullPointerException and How to fix it?

Hi,
Can someone please tell me what a Null Pointer Exception is and how to fix it?

Labels (2)
11 Replies
Anonymous
Not applicable
Author

A nullpointerexception occurs when a Java object references null....or nothing. A simple example is a String object. If we create a String object as below....
String myString = null;

....we have created a String object that references nothing. Since it references nothing, we cannot invoke any of a String's methods. If we do, we will get a NullPointerException. There is nothing to invoke. 

In order to prevent this in Talend, you can carry out some simple logic to prevent doing this. An example of this is below.

Lets assume I want to find the length of the String value, but I am not sure if it has a value. Let's assume the String is a column called myString. We need to check to see whether it is null first and then check the length. If it is null we will say the length is 0. This is done as below...
row1.myString!=null ? row1.myString.length() : 0

The above Java says "If row1.myStrng is not null, then use the length() method to retrieve the String length. Else, we will say the length is 0".
Anonymous
Not applicable
Author

Thanks,
I have scenario here where I have integers and floats in the csv file and it contains blanks ("") values in the column fields.

So I am transforming them in tMap. I have all of those designated columns as strings and transforming them as follows:

row1.Base_Price.equals.("") ? null : Integer.parseInt(row1.Base_Price)
or
row1.Bank_Payoff.equals("") ? null : Float.parseFloat(row1.Bank_Payoff)

I repeated the above procedures for most of the fields with null ("") values but I kept getting this error:

Java Exception: "Data Guy".......Float.parseFloat. 

I went back to check the Bank_Payoff and it was String being transformed into Float simple a 1-2-3 ETL job but after I couldn't get around this error I decided to convert everything back to string at the database level and see what the problem is:

Date                  Base_Price                Bank_Payoff                Salesman         
1/1/2017              123000                     235987.54                   Talend Guy
2/2/2018              353699                      1264.90                     ETL Guy
3/3/2016              654290                      Data Guy                   ............

I do not know what is going on here. 
Interesting part is when I changed all the designated columns to strings and imported the csv file to the database. The Bank_Payoff field when it sees a null ("") value, it takes the value from the next chronological column and displays as it's own value.

Now I decided to convert them back to integers and floats as I did previously and currently, I am running into NullPointerException Error.

Please provide some insights on this peculiar behavior of Talend.
Anonymous
Not applicable
Author

The problem is that if 
row1.Base_Price == null


Java is not able to evaluate function row1.Base_Price.equals()
Try to:
row1.Base_Price == null ? 0 : Integer.parseInt(row1.Base_Price)
Anonymous
Not applicable
Author

If.....
 row1.Bank_Payoff.equals("") ? null : Float.parseFloat(row1.Bank_Payoff) 

....is failing with the error you showed, it suggests that row1.Bank_Payoff is null and not an empty String (""). I just thought I would add that. Otherwise what silverhand has said is what I would do.
Anonymous
Not applicable
Author

Thanks guys, something funny is happening in Talend. It still shows me the Null Pointer Exception but not the field.









Exception in component tMap_1



java.lang.NullPointerException



at rehearsalproject.datainputcombo_0_1.Datainputcombo.tFileInputDelimited_1Process(Datainputcombo.java:3748)



at rehearsalproject.datainputcombo_0_1.Datainputcombo.runJobInTOS(Datainputcombo.java:4926)



at rehearsalproject.datainputcombo_0_1.Datainputcombo.main(Datainputcombo.java:4726)





[statistics] disconnected

Anonymous
Not applicable
Author

This isn't funny, it is pretty standard. You can find the problem by going to the code tab (bottom left hand corner of the screen) and going to line 3748 of the code. Can you show us that code and we can help you from there.
Anonymous
Not applicable
Author

Thanks, do you mean the code viewer....

Once I click on it I do not see any line numbers only columns I am trying to export into the database.
Anonymous
Not applicable
Author

No, the code tab which sits in the bottom left corner of the design window (Designer|Code|Jobscript) .....JobScript will only appear in the Enterprise Edition, otherwise it will show (Designer|Code).
Anonymous
Not applicable
Author

Thanks, I see the code now but I do not know how to get to the line Datainputcombo.java:3748 since the code is not numbered and the code contains lots of lines. Do you how I can have the line numbers to be displayed?

I appreciate your kind assistance!!!