Android Question Byte Array incompatible with Byte Array...

stefanoxjx

Active Member
Licensed User
Longtime User
Hi, I've a problem with 2 byte array.
I've first byte array named ActCode that have length of 800 bytes:
B4X:
Public const ActCode() As Byte = Array(0xc9, 0x44 ... )

I pass this array in a Sub as argument, and I need to attach this to another array.
B4X:
SendData(0x06, ActCode)

The function called is:
B4X:
Sub SendData(Type As Byte, PLoad() As Byte)
       Dim size as Int = ... Calculated about Type parameter
       Dim Pkt(Size) As Byte

       Pkt(0) = HEADER
       Pkt(1) = Type

       If PLoad <> Null Then bc.ArrayCopy(PLoad, 0, Pkt, 2, PLoad.Length)
       ...
EndSub

But I've always this error:
Error occurred on line: 336
java.lang.IllegalArgumentException: Array has incompatible type: class [B
at java.lang.reflect.Array.incompatibleType(Array.java:776)
at java.lang.reflect.Array.badArray(Array.java:785)
at java.lang.reflect.Array.setInt(Array.java:637)
at java.lang.reflect.Array.set(Array.java:468)
at anywheresoftware.b4a.shell.ArraysUtils.setElement(ArraysUtils.java:84)
at anywheresoftware.b4a.shell.Shell.setArrayElement(Shell.java:591)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:386)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6291)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

I tried too to substitute bc.ArrayCopy to:
B4X:
       If PLoad <> Null Then
              For I = 0 To PLoad.Length-1
                   Pkt(2+I) = PLoad(I)
             Next
       End If

and tried to make ActCode smaller then 800 bytes (2 bytes) but without any improvement.

I checked more times, and:
PLoad contains correct data.
Pkt is defined with correct size and sure defined as Byte array.
ActCode is defined as Byte array.

I'm sure I'm doing something wrong, but I don't understand what.
Can you help me?
Thanks.
 
Last edited:
Hi agraham, excuseme, but I've forgot to write that I've already tried to change "Type" in another word (ssType).
With execution step by step, the error appear when I execute:
B4X:
bc.ArrayCopy(PLoad, 0, Pkt, 2, PLoad.Length)
or
B4X:
Pkt(2+I) = PLoad(I) 'error even with i = 0
 
Upvote 0
Perhaps the error lies in the calculation of the length of Pkt, see my example
B4X:
Sub SendData(ssType As Byte, PLoad() As Byte)
       'Dim size as Int = ... Calculated about Type parameter
       Dim Pkt(PLoad.Length+2) As Byte

       Pkt(0) = HEADER
       Pkt(1) = ssType

       If PLoad <> Null Then bc.ArrayCopy(PLoad, 0, Pkt, 2, PLoad.Length)
       ...
EndSub
 
Last edited:
Upvote 0
I don't understand why PLoad.Length-2 ?
Anyway, from debug, I see that size is correctly valorized and Pkt have correct dimension.
 
Upvote 0
Sorry PLoad.Length+2
You are copying Pload to Pkt starting from the second position of pkt. So Pkt must be two bytes larger than Pload
 
Upvote 0
Ah ok, but in reality, "size" is calculated with more than 2 byte because besides PLoad I need to add another ten bytes.
I thought the problem could be overflow, but the error appears already when I=0, then in first byte of PLoad and in third byte of Pkt.
 
Upvote 0
try this
B4X:
Sub SendData(ssType As Byte, PLoad() As Byte)
       'Dim size as Int = ... Calculated about Type parameter
       Dim Pkt(PLoad.Length+2) As Byte
       Dim Hd (2) as Byte = array as byte (Header,ssType)

       If PLoad <> Null Then
              bc.ArrayCopy(PLoad, 0, Hd, 0, Hd.Length)
              bc.ArrayCopy(PLoad, 0, Pkt, 2, PLoad.Length)
       End if
EndSub
 
Upvote 0
In line
B4X:
bc.ArrayCopy(PLoad, 0, Hd, 0, Hd.Length)

I've this error:
It says that PLoad is not an array of Bytes but an array of objects

do it
B4X:
' Public const ActCode() As Byte = Array(0xc9, 0x44 ... ) ' WRONG
Public const ActCode() As Byte = Array as Byte(0xc9, 0x44 ... )
 
Last edited:
Upvote 0
Yes, but PLoad is instantiated and used only in SendData function.

B4X:
Sub SendData(packetType As Byte, [B]PLoad() As Byte[/B])

I don't understand how B4A can interprete PLoad as Array object.
Tomorrow I will do further tests.
 
Upvote 0
Yes, but PLoad is instantiated and used only in SendData function.

B4X:
Sub SendData(packetType As Byte, [B]PLoad() As Byte[/B])

I don't understand how B4A can interprete PLoad as Array object.
Tomorrow I will do further tests.
Maybe it depends on how the parameter you pass to SendData in global variables is declared
 
Upvote 0
The problem could be in your Const Byte Definition. You define a byte array but the ARRAY needs to be defined too!
B4X:
    Private const ActCode() As Byte=Array As Byte (1,2,3,4,5)

This code works:
Code:
Sub Process_Globals
    ...
    Private const ActCode() As Byte=Array As Byte (1,2,3,4,5)
    Private  bc As ByteConverter
End Sub

Sub DoIt(PLoad() As Byte )
    Dim PKT(PLoad.Length+10) As Byte
    bc.ArrayCopy(PLoad, 0, PKT, 5, PLoad.Length)
    For i=0 To PKT.Length-1
        Log( PKT(i) )
    Next
End Sub
 
Last edited:
Upvote 0
Public const ActCode() As Byte = Array(0xc9, 0x44 ... )
How does that even compile? I get an incompatible type error compilation error when I try :confused:

FOUND IT - There's a bug in the compiler I think. If you compile in Debug mode it accepts it. I don't use Debug mode so when I tried it I was in Release mode and got an error. I assume you must be compiling in Debug mode. This is one of the reasons I don't use Debug mode. I'll post a bug report
 
Upvote 0
Back
Top