0

I have a problem in my tMap when I try to do some operations on my string. I have a csv that has Ad_Set_Name that in some rows has more lines in the cell. I use the following:

row4.Ad_Set_Name.contains(" ") ? row4.Ad_Set_Name.substring(0,row4.Ad_Set_Name.indexOf(" ")) : row4.Ad_Set_Name
row4.Ad_Set_Name.contains("\"") ? row4.Ad_Set_Name.substring(row4.Ad_Set_Name.indexOf("\"")+1,row4.Ad_Set_Name.lastIndexOf("\"")) : "null"

I have let's say the Ad_Set_Name "Other vc_7days". So in this case, the first line will give me "Other" and the second one will give me "null". Ad_Set_Name = "Other vc_7days "something" 3rd" the first line will return "Other" and the second "something". But when I have Ad_Set_Name=

"Other

things" I have an error of index like: "StringIndexOutOfBoundsException: String index out of range: -1" Any idea why is that? Thanks a lot!

The error-log is:

Exception in component tMap_1 (facebook_campaigns_amazon_us)
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at mava2.facebook_campaigns_amazon_us_0_1.facebook_campaigns_amazon_us.tFileInputDelimited_2Process(facebook_campaigns_amazon_us.java:4649)
    at mava2.facebook_campaigns_amazon_us_0_1.facebook_campaigns_amazon_us.tWaitForFile_1Process(facebook_campaigns_amazon_us.java:2322)
    at mava2.facebook_campaigns_amazon_us_0_1.facebook_campaigns_amazon_us.tMysqlConnection_1Process(facebook_campaigns_amazon_us.java:856)
    at mava2.facebook_campaigns_amazon_us_0_1.facebook_campaigns_amazon_us.runJobInTOS(facebook_campaigns_amazon_us.java:5905)
    at mava2.facebook_campaigns_amazon_us_0_1.facebook_campaigns_amazon_us.main(facebook_campaigns_amazon_us.java:5575)
Mara M
  • 153
  • 1
  • 1
  • 10
  • Can you try printing out the row (`System.out.println(row4);`) on the line before the code you included in your question? That'll probably tell you where the problem is. If you can't figure it out, include the console output and I'll gladly help you. – TwiN Apr 27 '18 at 14:51
  • As soon as I go back to my pc I'll take some screen shots. And put them on weTransfer because I am not able yet to post pictures. Also I can't print the row because all of these happens in tMap when i link my csv output with my database. Thanks a lot! I'll post the pictures soon. – Mara M Apr 27 '18 at 15:02
  • i added the error log. – Mara M Apr 27 '18 at 15:15
  • I don't know where to put it. Because i do the code I've mentioned in tMap when linking. Is there a way I can print it first? I don't know. I'm new in this. – Mara M Apr 27 '18 at 15:22
  • So my error comes from tmap. And I only know to print from tJava element – Mara M Apr 27 '18 at 15:22
  • The code I posted is the Expression I type in the output from my tMap. It's not in a tJava, or tJavaRow. So I don't know how to print it. – Mara M Apr 27 '18 at 15:43
  • mava2.facebook_campaigns_amazon_us_0_1.facebook_campaigns_amazon_us$row2Struct@396a51ab[Ad_Set_Name=null,Delivery=null,Reporting_Starts=null,Reporting_Ends=null,Amount_Spent__USD_=null,CPC__All___USD_=null,CTR__All_=null,Website_Purchases_Conversion_Value=null,Website_Purchase_ROAS__Return_on_Ad_Spend_=null] – Mara M Apr 27 '18 at 15:45
  • It's the only thing I could print. – Mara M Apr 27 '18 at 15:46
  • But this output is before I transfer the data. My job is like tFileInputDelimited----->tJava------>tMap---->tMysqlOutput. So I print in tJava and the code I posted is in tMap. – Mara M Apr 27 '18 at 15:48
  • Can you add some of the code before and after the first block of code in your question? (Where does it come from) – TwiN Apr 27 '18 at 15:50
  • I don't have code before or after. It is a Talend Open Studio project with the components I told you about. – Mara M Apr 27 '18 at 16:02

2 Answers2

0
java.lang.StringIndexOutOfBoundsException: String index out of range: -1

tells us that there's a non-null string that does not contain the character you're looking for.

In your case, it can happen when there is either (whitespace) or ".

Reproducing it using Java would look like this:

String test = "\"Other \n\nthings\"";

test = test.contains(" ") ? test.substring(0, test.indexOf(" ")) : test;

System.out.println(test); // "Other


System.out.println(test.contains("\"") ?
     test.substring(test.indexOf("\"")+1,test.lastIndexOf("\"")) : "null"); // error!

You're getting an error because by the time your second validation runs, the string is "Other, meaning that

test.contains("\"") ? test.substring(test.indexOf("\"")+1,test.lastIndexOf("\"")) : "null"

actually resolves to

test.contains("\"") ? test.substring(0+1, 0) : "null"

And as specified by the javadoc

IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

In your case, the beginIndex is 1 and the endIndex is 0, which is why StringIndexOutOfBoundsException is thrown.

To prevent that error from being throw, instead of using

row4.Ad_Set_Name.contains("\"")

use

row4.Ad_Set_Name.indexOf('"', 2) != -1

That will make sure that your string has at least 2 occurrences of the character ".

TwiN
  • 3,554
  • 1
  • 20
  • 31
  • It can be what you say but in my case still doesn't work. I do not have any "\"" in my string and still fails for intex – Mara M Apr 30 '18 at 05:20
0

I solved the problem. In the tFileInputDelimited element I didn't checked the CSV Options. Because Talend reads the cell with multiple line with "", I had to have the escape char set on "\"".

Mara M
  • 153
  • 1
  • 1
  • 10