Android Question java.lang.ArrayIndexOutOfBound

BPlagens

New Member
Licensed User
Longtime User
Hello,

after a long time without b4a i start a new project.

As start point i use the "Bluetooth.zip" from https://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/

The original works fine.

But when i change the "Sub btnSend_Click" to:

B4X:
Sub btnSend_Click
    'AStream.Write(txtInput.Text.GetBytes("UTF8"))
    If AStream.IsInitialized = False Then Return
   
    Dim buffer() As Byte
    'buffer = EditText1.Text.GetBytes("UTF8")
    buffer(0) = 0x1b
    buffer(1) = 0x40
    buffer(2) = 0x00
   
    'AStream.Write(buffer)
    AStream.Write2(buffer,0,3)
   
    txtInput.SelectAll
    txtInput.RequestFocus
    LogMessage("Me", txtInput.Text)
End Sub

I get the Error: "java.lang.ArrayIndexOutOfBound sException: length=0; index=0"

There is no different in using .Write or .Write2

What i am doing wrong?

Best regards
Bernd
 

DonManfred

Expert
Licensed User
Longtime User
Dim buffer() As Byte
You cannot define an empty array.
B4X:
dim buffer(3) as Byte

Edit: You CAN define a empty array, but you can´t access the bytes as long the array length is not known.
 
Last edited:
Upvote 0

BPlagens

New Member
Licensed User
Longtime User
Hi,

thank you for this quick response! :)
You where absolutly right!

I was using this syntax which did not work:
B4X:
Dim buffer(0 To 2) As Byte

Thank you! :)
 
Upvote 0

BPlagens

New Member
Licensed User
Longtime User
Its me again - just a little help please:

When i search the Forum i found some Examples (eg: https://www.b4x.com/android/forum/threads/sql-tutorial.6736/#content or https://www.b4x.com/android/forum/threads/asyncstreams-tutorial.7669/#content or https://www.b4x.com/android/forum/threads/send-or-receive-bytes-via-bluetooth.46258/#post-285708)
Which use also this syntax:

B4X:
 Dim Buffer() As Byte 'declares an empty array

So please at which point can i use "Buffer()" and when i can use "Buffer(3)" ?

Best Regards
Bernd
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
In the line after the empty array you set a new content (and length!) with
B4X:
Buffer = OutputStream1.ToBytesArray
After this line the buffer have the size of the content of the outputstream.

B4X:
buffer = EditText1.Text.GetBytes("UTF8")
will do the same with the content of an edittext

But as you want to send exactly three bytes you should define a correct array first. Either using
B4X:
Dim Buffer() As Byte
when you set the content from an stream or
B4X:
Dim Buffer(3) As Byte
if you want to define the content by yourself
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The problematic in your code
B4X:
Dim buffer() As Byte
buffer(0) = 0x1b
is that you want to ACCESS a specific byte in
B4X:
    buffer(0) = 0x1b
but at this point the array is still EMPTY because you did not fill the array from a stream and you did not define a specific length.
 
Upvote 0

BPlagens

New Member
Licensed User
Longtime User
Hello Manfred

Thank you very much for this explanation! :)

Just a last question:
Why did:
B4X:
Dim buffer(0 To 2) As Byte
do not work but:
B4X:
Dim Buffer(3) As Byte
is ok?

I would assume, that in both variants the array have 3-elements or?

Right now i am a little disappointed that i do not get a message from the Compiler/Editor.
When i type in the same Syntax in VS2013 i get a Message like: "buffer-Variable is used before a value is assigned"

Once again - thank you!
BR Bernd
 
Upvote 0
Top