B4J Question StringIndexOutOfBoundsException when not doing string operations

JackKirk

Well-Known Member
Licensed User
Longtime User
I am doing some fiddling with the Windows routing table, the problem occurs in this code:
B4X:
Private wrk_sh As Shell
 
'For all connections found...
For wrk_ptr = 0 To Gen_load_balancing_connections.Size - 1

    'Set up shell to set adapter metric of connection to 5
    wrk_args.Clear
    wrk_args.Add("/c")
    wrk_args.Add("powershell.exe set-netroute -interfacealias '" & Gen_load_balancing_connections.Get(wrk_ptr) & "' -routemetric 5")
       
    wrk_sh.InitializeDoNotHandleQuotes("Event_shell2", "cmd.exe", wrk_args)
                                   
    Try
                                       
        'Run shell with a 10 min timeout
        wrk_sh.Run(600000)
       
        'Wait for shell to terminate...
        Wait For Event_shell2_ProcessCompleted(Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
       
        If Success And ExitCode = 0 Then

        Else
       
            wrk_sms = wrk_sms & CRLF & Gen_load_balancing_connections.Get(wrk_ptr) & " - error setting adapter metric"
           
            wrk_log = wrk_log & Gen_crlf & "Error after shell wait (setting adapter metric)..."
                                                   
            wrk_log = wrk_log & Gen_crlf & "Success: " & Success & Gen_crlf & "ExitCode: " & ExitCode & Gen_crlf & "StdOut: " & StdOut & Gen_crlf & "StdErr: " & StdErr
   
        End If
                   
    Catch
       
        wrk_sms = wrk_sms & CRLF & Gen_load_balancing_connections.Get(wrk_ptr) & " - error setting adapter metric"
       
        wrk_log = wrk_log & Gen_crlf & "Error at shell run (setting adapter metric)..."
   
    End Try

Next
When running in Debug mode, the error I am getting is:
Waiting for debugger to connect...
Program started.
Error occurred on line: 644 (Main)
java.lang.StringIndexOutOfBoundsException: String index out of range: -2
at java.base/java.lang.String.substring(String.java:1840)
at b4j.example.main$ResumableSub_AppStart.resume(main.java:2069)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:628)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:168)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:135)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:84)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA$3.run(BA.java:246)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
at java.base/java.lang.Thread.run(Thread.java:844)

But line 644 is:

If Success And ExitCode = 0 Then

If I put some nonsense code such as:
B4X:
If Success And ExitCode = 0 Then
    Private junk As Int = wrk_ptr
    wrk_ptr = junk
Else
It then fails at:

wrk_ptr = junk

I have tried all sorts of alternatives but am still generating this error.

Having run out of ideas I implore someone to tell me what is going on.

Thanks in anticipation...
 
Last edited:

JackKirk

Well-Known Member
Licensed User
Longtime User
After some more fiddling, if I actually use a string operation in the code:
B4X:
If Success And ExitCode = 0 Then
              
    wrk_log = wrk_log & Gen_crlf & "Adapter metric set"

Else
It works, this is beyond bizarre...

I think this is a bug so have reported it to the bug section.
 
Last edited:
Upvote 0
Top