Android Question How can I initialise this array?

Arf

Well-Known Member
Licensed User
Longtime User
Apologies if it's a stupid question, but I can't seem to get this right.

I have a custom type that contains an array of a variable number of another custom type:
B4X:
Type MemoryRegionStruct(MemoryRegionType As Byte, Address As Int, Size As Int)
Type QueryResultsStruct(Command As Byte, BytesPerPacket As Byte, DeviceFamilyType As Byte, MemoryRegions() As MemoryRegionStruct)

MemoryRegions() is the problem. When I pull data out of an incoming USB packet to populate into MemoryRegion(0), the MemoryRegions array is null and I get an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference

I am actually populating a single instance of a MemoryRegion and then trying to copy it to the the QueryResultsStruct, like this:

B4X:
        Do While memtype <> -1
            Dim memreg As MemoryRegionStruct
            memreg.Initialize
            ref = ref + 1
            memreg.MemoryRegionType = memtype
            memreg.Address = FourBytesToInt(Request.Buffer(ref), Request.Buffer(ref+1), Request.Buffer(ref+2), Request.Buffer(ref+3))
            ref = ref + 4
            memreg.Size = FourBytesToInt(Request.Buffer(ref), Request.Buffer(ref+1), Request.Buffer(ref+2), Request.Buffer(ref+3))
            ref = ref + 4
            memtype = Request.Buffer(ref)
            'myResponse.MemoryRegions(regionref).Initialize
            myResponse.MemoryRegions(regionref) = memreg
            regionref = regionref + 1
        Loop

The error occurs on the 2nd last line within the loop, I tried to initialise the Memory (commented out line) but that too results in an error:
java.lang.NullPointerException: array == null

Any guidance gratefully received.
 

Arf

Well-Known Member
Licensed User
Longtime User
Thanks, tried but it won't compile - but that field should be a byte, not an array of bytes - the array should be an array of MemoryRegionStructs.
I will simplify the loop I wrote above and remove some things for clarity:
B4X:
Dim myResponse As QueryResultsStruct
Do While regions < 3
            Dim memreg As MemoryRegionStruct
            memreg.Initialize
            memreg.MemoryRegionType = fetchType(regions)
            memreg.Address = fetchAddress(regions)
            memreg.Size = fetchSize(regions)

            'myResponse.MemoryRegions(regions).Initialize
            myResponse.MemoryRegions(regions) = memreg
            regions = regions + 1
Loop
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
When you initialized your "myResponse" variable, how did you declare the MemoryRegions array? It's variable length so you will need to DIM it somewhere to a fixed size. If your length will truly be variable then I suggest using a LIST instead of an array. If the length is fixed, you could create an initialization routine:
B4X:
'....
    Dim poQuery As QueryResultsStruct = NewQueryStru
'....
Public Sub NewQueryStru As QueryResultsStruct
    Dim poQ As QueryResultsStruct
    poQ.Initialize
    Dim poRegions(3) As MemoryRegionStruct
    poQ.MemoryRegions = poRegions
    Return poQ
End Sub
It wouldn't be much to add a length parameter to this function to size the region if necessary. If it is always fixed at 3 then just change your type declaration and the .Initialize for the QueryResultStruct variable will handle it.
 
Last edited:
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Ah OK, now that you mention it I remember using a list last time I had a similar issue.
In this case though the number of array elements will never be more than 3 so I'll change the array to a fixed size of 3.
- just did, working. Thanks all.
 
Upvote 0
Top