Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
Canabral
Contributor
Contributor

Split / ArrayIndexOutOfBoundsException

Hello,

I have a csv file:

COL1|COL2|COL3

avant|99999;88888;77777|après

I want to split column 2.

So I did in a JavaRow:

output_row.COL1 = input_row.COL1;

output_row.COL2_1 = StringHandling.TRIM(input_row.COL2).split(";")[0];

output_row.COL2_2 = StringHandling.TRIM(input_row.COL2).split(";")[1];

output_row.COL2_3 = StringHandling.TRIM(input_row.COL2).split(";")[2];

output_row.COL3 = input_row.COL3;

But I have this error: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1

it's ok only with

output_row.COL2_1 = StringHandling.TRIM(input_row.COL2).split(";")[0];

I don't understand because I have 3 elements and if I test with

StringHandling.TRIM(input_row.COL2).split(";").length,

it turns out 3 for me...

Thanks for your help.

Labels (2)
1 Solution

Accepted Solutions
SadlerS
Contributor III
Contributor III

Hello,

The error message you're receiving, "java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1," indicates that you're trying to access mymilestonecard an index in an array that doesn't exist. In this case, it means that the array resulting from the split(";") operation has a length of 1, not 3 as you expected.

 

The reason for this issue is that the delimiter you're using in the split function is incorrect. Looking at your CSV file example, the values in the second column are separated by a semicolon followed by a space ("; "), not just a semicolon (";").

 

To fix the problem, you should update your code to split the column by "; " instead of ";". Here's the modified code:

output_row.COL1 = input_row.COL1;

String[] col2Values = StringHandling.TRIM(input_row.COL2).split("; ");

output_row.COL2_1 = col2Values.length >= 1 ? col2Values[0] : "";

output_row.COL2_2 = col2Values.length >= 2 ? col2Values[1] : "";

output_row.COL2_3 = col2Values.length >= 3 ? col2Values[2] : "";

output_row.COL3 = input_row.COL3;

This code uses "; " as the delimiter and checks the length of the resulting array to ensure that you don't go out of bounds when accessing the elements. If the array length is less than the index being accessed, it assigns an empty string ("") to the corresponding output column.

 

Make sure to update the delimiter in the split function to match the actual delimiter in your CSV file.

 

View solution in original post

4 Replies
Anonymous
Not applicable

Hello @Simon Thieffry​ ,

I can't reproduce the issue in my side, could you please provide the whole job execution error log for more investigation? thanks

 

Best regards

Aiming

Canabral
Contributor
Contributor
Author

Hello,

 

here is the error log:

 

Démarrage du Job test à 10:31 04/05/2023.

[statistics] connecting to socket on port 3699

[statistics] connected

Exception in component tJavaRow_1 (test)

java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1

at godin.test_0_1.test.tFileInputDelimited_1Process(test.java:1059)

at godin.test_0_1.test.runJobInTOS(test.java:1513)

at godin.test_0_1.test.main(test.java:1351)

[statistics] disconnected

 

Job test terminé à 10:31 04/05/2023. [Code de sortie = 1]

 

And more info on the job in the capture.

 

Thanks.

 

Simon

 

SadlerS
Contributor III
Contributor III

Hello,

The error message you're receiving, "java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1," indicates that you're trying to access mymilestonecard an index in an array that doesn't exist. In this case, it means that the array resulting from the split(";") operation has a length of 1, not 3 as you expected.

 

The reason for this issue is that the delimiter you're using in the split function is incorrect. Looking at your CSV file example, the values in the second column are separated by a semicolon followed by a space ("; "), not just a semicolon (";").

 

To fix the problem, you should update your code to split the column by "; " instead of ";". Here's the modified code:

output_row.COL1 = input_row.COL1;

String[] col2Values = StringHandling.TRIM(input_row.COL2).split("; ");

output_row.COL2_1 = col2Values.length >= 1 ? col2Values[0] : "";

output_row.COL2_2 = col2Values.length >= 2 ? col2Values[1] : "";

output_row.COL2_3 = col2Values.length >= 3 ? col2Values[2] : "";

output_row.COL3 = input_row.COL3;

This code uses "; " as the delimiter and checks the length of the resulting array to ensure that you don't go out of bounds when accessing the elements. If the array length is less than the index being accessed, it assigns an empty string ("") to the corresponding output column.

 

Make sure to update the delimiter in the split function to match the actual delimiter in your CSV file.

 

Canabral
Contributor
Contributor
Author

Hello,

 

No, there is no space in my separator, that is semicolon.

 

But using the code with the intermediate variable it works :

 

String[] col2Values = StringHandling.TRIM(input_row.COL2).split("; ");

output_row.COL2_1 = col2Values.length >= 1 ? col2Values[0] : "";

output_row.COL2_2 = col2Values.length >= 2 ? col2Values[1] : "";

output_row.COL2_3 = col2Values.length >= 3 ? col2Values[2] : "";

 

Thanks.

 

Simon