B4J Question SubString problem

HARRY

Active Member
Licensed User
Longtime 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
 

HARRY

Active Member
Licensed User
Longtime 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.



B4X:
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)
 
Upvote 0

HARRY

Active Member
Licensed User
Longtime 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
 
Upvote 0
Top