1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

B4J Question SubString problem

Discussion in 'B4J Questions' started by HARRY, Mar 20, 2015.

  1. HARRY

    HARRY Active Member Licensed User

    Hi,

    I use the following code:

    Sub AStream_NewData (Buffer() As Byte)
    Dim s As String
    Dim l As String
    s= BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(Buffer.Length)
    Log(s)
    l=s.SubString2(0,1)
    Log(l)
    If l=1 Then......

    The Buffer.length is reported to be 32 as well as the s.length.

    On l=s.SubString2(0,1) the program hangs a while and then announces: Error occurred on line 141 (main). This is the End Sub line of Sub AStream_NewData (Buffer() As Byte)

    Debugging aborts


    What am I doing wrong?

    Harry
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.

    Can you post the full error message from the logs?
     
  3. HARRY

    HARRY Active Member Licensed User

    Sorry Erel,

    Now the code as code and the log. May be that the first byte of the buffer and of s is not printable. i don't know whether that matters.



    Code:
    Sub AStream_NewData (Buffer() As Byte)
    Dim b, s As String
    Dim newhour As Int
    Dim l As Char
    Dim i As Int
        s= 
    BytesToString(Buffer, 0, Buffer.Length, "UTF8")
        
    Log(Buffer.Length)
        
    Log (s.Length)
        
    Log(s)
        l=s.substring2(
    0,1)
        
    Log(l)

    Program started.
    (ArrayList) [/dev/ttyAMA0, /dev/ttyUSB0]
    0
    0
    Error occurred on line: 77 (main).
    java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.substring(String.java:1950)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:563)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:221)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:156)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:82)
    at anywheresoft
    ware.b4a.BA$3.run(BA.java:178)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
    at anywheresoftware.b4a.ShellBA.startMessageLoop(ShellBA.java:103)
    at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:131)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:292)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:156)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:82)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.main(main.java:29)
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can see from the logs that the string is empty. Why are you converting the data to a string? What is the source of this data?
    Only text data should be converted to string.
     
  5. HARRY

    HARRY Active Member Licensed User

    Sorry again,

    The source of the data is a mixture of words and a structure containing integers, Strings and bytes. Decoding it is too difficult for me. Meanwhile I have changed the set up. The source now is just a String and I am going to use AsyncStreamsText, avoiding a lot of pitfalls.

    Harry
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice