Android Question [SOLVED] Custom Types, Byte arrays and Null Pointers...

Straker

Active Member
Licensed User
Longtime User
Hi everybody, ho someone can help me...

I'm writing a program witjh some data exchange between the device and the PC through a socket and AsyncStream. Everything is fine if I maintain the exchanged data very simple.

But when I try to use some custom Type, just to make the code more readable, I've got always the same Null Pointer Exception error...

The code is as follows:

B4X:
    Type HeaderType(Stx1 As Byte, Stx2 As Byte, Orig As Byte, Dest As Byte, cmd As Byte, length As Byte, Addr As Long, chkSum As Byte,)
        Type RxBuffType(Header as HeaderType, Dati(127) As Byte, chkDati As Byte)
    Dim RxBuff As RxBuffType

Through AsyncStream the program receives a Buffer of bytes. Bytes from 0 to 10 are the header, from the 11th byte are the real Data. Is not a problem of the index. I made some test checking the Buffer.length and always starting from index=0

QUESTION 1: there is a way to map the first 11 bytes from Buffer and 'map' them on the header. I know B4a doesn't support pointers, but there is a workaround just like in Visual basic (memcpy + VarPtr)?

QUESTION 2: How can I copy bytes from Buffer() to RxBuff.Dati() ?

For copying bytes, I tried the following codes, bu without effort:

B4X:
   dim b as byte  
   rxBuff.Stx1=Buffer(0)    ' WORKS! ok, of course
   b=Buffer(0)
   rxBuff.Stx1=b            'WORKS! of course...
  
  rxBuff.Dati(0) =Buffer(0) 'CRASH! why?

  b=Buffer(0)
  rxBuff.Dati(0)=b          'CRASH! 

   dim bc as ByteConverter
   bc.ArrayCopy(Buffer, 0, RxBuff.Dati, 1) 'CRASH rxBuff.Dati -> null pointer
 

derez

Expert
Licensed User
Longtime User
Two points:
1. It should be RxBuff.Header.Stx1 because stx1 belongs to the header.
2. initialize the type variable before working with it: RxBuff.Initialize
Then try again the ArrayCopy
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
Two points:
1. It should be RxBuff.Header.Stx1 because stx1 belongs to the header.
Yes, it was just a copy'n'paste error (from one try to another)

2. initialize the type variable before working with it: RxBuff.Initialize
Then try again the ArrayCopy

THANK YOU, i simply missed that I should initialize the custom types (why? who cares, it works!)
 
Upvote 0
Top