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:
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:
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.
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.