Android Question Substring2 Error java.lang.StringIndexOutOfBoundsException: length=6773; reg...

Tayfur

Well-Known Member
Licensed User
Hello .
I used sbstring for parshing.
So, i read a error. whrere id problem. I know it easy , So I can t see??


thank you for helps.


B4X:
Sub Button1_Click
'Send a GET request
   job3.Initialize("Job3", Me)
   job3.Download("http://www.tcmb.gov.tr/kurlar/today.xml")
  
  
End Sub


Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   Dim tcmb As String
   Dim USD As String
   If Job.Success = True Then
     Log(Job.GetString)
    tcmb = Job.GetString
    tcmb=tcmb.SubString(200)
            Log("result1=" & tcmb.Length) '6773
         Log("result2=" & tcmb.IndexOf("US DOLLAR</CurrencyName><ForexBuying>")) '74
        
        USD= tcmb.SubString2(74,30)
    'Log(tcmb.SubString2(tcmb.IndexOf("US DOLLAR</CurrencyName><ForexBuying>"),4))
    
Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
   Job.Release
End Sub

** Service (httputils2service) Create **
** Service (httputils2service) Start **
JobName = Job3, Success = true
Message longer than Log limit (4000). Message was truncated.
<?xml version="1.0" encoding="ISO-8859-9"?>

<?xml-stylesheet type="text/xsl" href="isokur.xsl"?>

<Tarih_Date Tarih="16.12.2014" Date="12/16/2014" Bulten_No="2014/238"><Currency CrossOrder="0" Kod="USD" CurrencyCode="USD"><Unit>1</Unit><Isim>ABD DOLARI</Isim><CurrencyName>US DOLLAR</CurrencyName><ForexBuying>2.3671</ForexBuying><ForexSelling>2.3714</ForexSelling><BanknoteBuying>2.3654</BanknoteBuying><BanknoteSelling>2.375</BanknoteSelling><CrossRateUSD>1</CrossRateUSD><CrossRateOther></CrossRateOther></Currency><Currency CrossOrder="1" Kod="AUD" CurrencyCode="AUD"><Unit>1</Unit><Isim>AVUSTRALYA DOLARI</Isim><CurrencyName>AUSTRALIAN DOLLAR</CurrencyName><ForexBuying>1.947</ForexBuying><ForexSelling>1.9597</ForexSelling><BanknoteBuying>1.938</BanknoteBuying><BanknoteSelling>1.9715</BanknoteSelling><CrossRateUSD>1.2129</CrossRateUSD><CrossRateOther></CrossRateOther></Currency><Currency CrossOrder="2" Kod="DKK" CurrencyCode="DKK"><Unit>1</Unit><Isim>DAN�MARKA KRONU</Isim><CurrencyName>DANISH KRONE</CurrencyName><ForexBuying>0.39721</ForexBuying><ForexSelling>0.39916</ForexSelling><BanknoteBuying>0.39693</BanknoteBuying><BanknoteSelling>0.40008</BanknoteSelling><CrossRateUSD>5.9502</CrossRateUSD><CrossRateOther></CrossRateOther></Currency><Currency CrossOrder="9" Kod="EUR" CurrencyCode="EUR"><Unit>1</Unit><Isim>EURO</Isim><CurrencyName>EURO</CurrencyName><ForexBuying>2.9601</ForexBuying><ForexSelling>2.9654</ForexSelling><BanknoteBuying>2.958</BanknoteBuying><BanknoteSelling>2.9698</BanknoteSelling><CrossRateUSD></CrossRateUSD><CrossRateOther>1.2505</CrossRateOther></Currency><Currency CrossOrder="10" Kod="GBP" CurrencyCode="GBP"><Unit>1</Unit><Isim>�NG�L�Z STERL�N�</Isim><CurrencyName>POUND STERLING</CurrencyName><ForexBuying>3.7083</ForexBuying><ForexSelling>3.7277</ForexSelling><BanknoteBuying>3.7057</BanknoteBuying><BanknoteSelling>3.7333</BanknoteSelling><CrossRateUSD></CrossRateUSD><CrossRateOther>1.5693</CrossRateOther></Currency><Currency CrossOrder="3" Kod="CHF" CurrencyCode="CHF"><Unit>1</Unit><Isim>�SV��RE FRANGI</Isim><CurrencyName>SWISS FRANK</CurrencyName><ForexBuying>2.4591</ForexBuying><ForexSelling>2.4749</ForexSelling><BanknoteBuying>2.4554</BanknoteBuying><BanknoteSelling>2.4786</BanknoteSelling><CrossRateUSD>0.9604</CrossRateUSD><CrossRateOther></CrossRateOther></Currency><Currency CrossOrder="4" Kod="SEK" CurrencyCode="SEK"><Unit>1</Unit><Isim>�SVE� KRONU</Isim><CurrencyName>SWEDISH KRONA</CurrencyName><ForexBuying>0.30955</ForexBuying><ForexSelling>0.31275</ForexSelling><BanknoteBuying>0.30933</BanknoteBuying><BanknoteSelling>0.31347</BanknoteSelling><CrossRateUSD>7.6145</CrossRateUSD><CrossRateOther></CrossRateOther></Currency><Currency CrossOrder="6" Kod="CAD" CurrencyCode="CAD"><Unit>1</Unit><Isim>KANADA DOLARI</Isim><CurrencyName>CANADIAN DOLLAR</CurrencyName><ForexBuying>2.0288</ForexBuying><ForexSelling>2.038</ForexSelling><BanknoteBuying>2.0213</BanknoteBuying><BanknoteSelling>2.0457</BanknoteSelling><CrossRateUSD>1.1652</CrossRateUSD><CrossRateOther></CrossRateOther></Currency><Currency CrossOrder="11" Kod="KWD" CurrencyCode="KWD"><Unit>1</Unit><Isim>KUVEYT D�NARI</Isim><CurrencyName>KUWAITI DINAR</CurrencyName><ForexBuying>8.0722</ForexBuying><ForexSelling>8.1778</ForexSelling><BanknoteBuying>7.9511</BanknoteBuying><BanknoteSelling>8.3005</BanknoteSelling><CrossRateUSD></CrossRateUSD><CrossRateOther>3.4293</CrossRateOther></Currency><Currency CrossOrder="7" Kod="NOK" CurrencyCode="NOK"><Unit>1</Unit><Isim>NORVE� KRONU</Isim><CurrencyName>NORWEGIAN KRONE</CurrencyName><ForexBuying>0.31211</ForexBuying><ForexSelling>0.31421</ForexSelling><BanknoteBuying>0.31189</BanknoteBuying><BanknoteSelling>0.31493</BanknoteSelling><CrossRateUSD>7.5657</CrossRateUSD><CrossRateOther></CrossRateOther></Currency><Currency CrossOrder="8" Kod="SAR" CurrencyCode="SAR"><Unit>1</Unit><Isim>SUUD� ARAB�STAN R�YAL�</Isim><CurrencyName>SAUDI RIYAL</CurrencyName><ForexBuying>0.63069</ForexBuying><ForexSelling>0.63182</ForexSelling><BanknoteBuying>0.62596</Banknot
Message longer than Log limit (4000). Message was truncated.
result1=6773
result2=74
Error occurred on line: 224 (main)
java.lang.StringIndexOutOfBoundsException: length=6773; regionStart=74; regionLength=-44
at java.lang.String.startEndAndLength(String.java:593)
at java.lang.String.substring(String.java:1474)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA$3.run(BA.java:320)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
 

sorex

Expert
Licensed User
try

B4X:
USD= tcmb.SubString2(74,74+30)
oh, and xml is supposed to be handled as xml, not plain text.
so you better use an xml parser to grab the data.
 

klaus

Expert
Licensed User
The help tells you SubString2(BeginIndex As Int, EndIndex As Int).
Returns a new string which is a substring of the original string.
The new string will include the character at BeginIndex and will extend to the character at EndIndex, not including the last character.

In your case, USD= tcmb.SubString2(74,30) the EndIndex is smaller than the BeginIndex !
 

sorex

Expert
Licensed User
I understand his confusion, in a lot of languages the 2nd parameter is the lenght and not the end position in the string.
 

Tayfur

Well-Known Member
Licensed User
The help tells you SubString2(BeginIndex As Int, EndIndex As Int).
Returns a new string which is a substring of the original string.
The new string will include the character at BeginIndex and will extend to the character at EndIndex, not including the last character.

In your case, USD= tcmb.SubString2(74,30) the EndIndex is smaller than the BeginIndex !

oohhh ....no :eek::eek::eek::eek:

ı am using .substring2(beginindex, Length) ;)


thank you Klaus.
 
Top