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

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Moro
Contributor
Contributor

tmap trimming an entry - problem when entry is empty (cannot invoke "String.substring(int,int)" because "row.A" is null)

Hi all!

I have been trying to trim an entry in a table but I keep getting the following error:

Cannot invoke "String.substring(int,int)" because "row.A" is null

The way I did it:

In tmap I tried to check first if the entry is empty and then do the trimming like the following

row.A == null ? row.A : (row.A.length() >10 ? StringHandling.LEFT(row.A,4):row.A)

It is not working. Any suggestions from your side?

Thank you!

Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable

This is caused by the logic here.....

 

((row3.newColumn1== null) || ("".equals(row3.newColumn1 )) || row3.newColumn1.isEmpty()) ? (row3.newColumn1.length()>7 ? StringHandling.LEFT(row3.newColumn1,7): row3.newColumn1) : row3.newColumn1 

 

This is saying....

 

IF row3.newColumn1 is null OR row3.newColumn1 is an empty String OR row3.newColumn1 is empty THEN

IF row3.newColumn1 is longer than 7 characters THEN .... do the rest.

 

If row3.newColumn1 is null you are allowing the code to attempt to use the length() method on a null object. This is not possible.

 

I think you need.....

 

((row3.newColumn1== null) || ("".equals(row3.newColumn1 )) || row3.newColumn1.isEmpty()) ? row3.newColumn1 : row3.newColumn1.length()>7 ? StringHandling.LEFT(row3.newColumn1,7): row3.newColumn1

 

 

View solution in original post

10 Replies
Anonymous
Not applicable

What do you get when you run your code where you check for null?

Moro
Contributor
Contributor
Author

I only get the error message as written above.

I thought I am checking für null by

row.A == null ? row.A : (row.A.length() >10 ? StringHandling.LEFT(row.A,4):row.A)

Anonymous
Not applicable

That code should work. Can you post the exact error message you get? This could be related to another column.

Moro
Contributor
Contributor
Author

Exception in component tMap_3 (Trim)

java.lang.NullPointerException: Cannot invoke "String.length()" because "row.A" is null

at project_1.trim_0_1.Trim.tFileInputExcel_3Process(Trim.java:1967)

at project_1.trim_0_1.Trim.runJobInTOS(Trim.java:2555)

at project_1.trim_0_1.Trim.main(Trim.java:2393)

Anonymous
Not applicable

Can you show me a screenshot of your tMap configuration please? I'll need to see every output expression.

Moro
Contributor
Contributor
Author

Hi

here is the screenshot for this error message ( column A type: string)

Exception in component tMap_3 (test_TrimSTring)

java.lang.NullPointerException: Cannot invoke "String.substring(int, int)" because "row3.A" is null

at project_1.test_trimstring_0_1.test_TrimSTring.tFileInputExcel_3Process(test_TrimSTring.java:1588)

at project_1.test_trimstring_0_1.test_TrimSTring.runJobInTOS(test_TrimSTring.java:2042)

at project_1.test_trimstring_0_1.test_TrimSTring.main(test_TrimSTring.java:1880)

 

 

0695b00000WubRnAAJ.pngI also tried

(row3.A== null) || ("".equals(row3.A)) || row3.A.isEmpty() ? row3.newColumn1.length()>7 ? StringHandling.LEFT(row3.A,7): row3.A : row3.A 

and with brackets

((row3.A== null) || ("".equals(row3.A)) || row3.A.isEmpty() )? (row3.newColumn1.length()>7 ? StringHandling.LEFT(row3.A,7): row3.A 😞 row3.A 

 

Error message:

Exception in component tMap_3 (test_TrimSTring)

java.lang.NullPointerException: Cannot invoke "String.substring(int, int)" because "row3.A" is null

at project_1.test_trimstring_0_1.test_TrimSTring.tFileInputExcel_3Process(test_TrimSTring.java:1588)

at project_1.test_trimstring_0_1.test_TrimSTring.runJobInTOS(test_TrimSTring.java:2042)

at project_1.test_trimstring_0_1.test_TrimSTring.main(test_TrimSTring.java:1880)

 

0695b00000WubRiAAJ.png 

 

 

Anonymous
Not applicable

You have at least 2 other tMap Var expressions using the column row3.A without a check for null. I believe that is your problem here.

Moro
Contributor
Contributor
Author

Hi

thanks for the tipp.

It still doesnt work properly: It works for row3.newColumn but eg not for row3.newColumn4 or row3.newColumn7. I have no idea why.

in tmap:

((row3.newColumn1== null) || ("".equals(row3.newColumn1 )) || row3.newColumn1.isEmpty()) ? (row3.newColumn1.length()>7 ? StringHandling.LEFT(row3.newColumn1,7): row3.newColumn1) : row3.newColumn1 

 

Error:

Exception in component tMap_3 (B_TrimSTring)

java.lang.NullPointerException: Cannot invoke "String.length()" because "row3.newColumn1" is null)0695b00000WugqdAAB.png

Anonymous
Not applicable

This is caused by the logic here.....

 

((row3.newColumn1== null) || ("".equals(row3.newColumn1 )) || row3.newColumn1.isEmpty()) ? (row3.newColumn1.length()>7 ? StringHandling.LEFT(row3.newColumn1,7): row3.newColumn1) : row3.newColumn1 

 

This is saying....

 

IF row3.newColumn1 is null OR row3.newColumn1 is an empty String OR row3.newColumn1 is empty THEN

IF row3.newColumn1 is longer than 7 characters THEN .... do the rest.

 

If row3.newColumn1 is null you are allowing the code to attempt to use the length() method on a null object. This is not possible.

 

I think you need.....

 

((row3.newColumn1== null) || ("".equals(row3.newColumn1 )) || row3.newColumn1.isEmpty()) ? row3.newColumn1 : row3.newColumn1.length()>7 ? StringHandling.LEFT(row3.newColumn1,7): row3.newColumn1