Android Question NullPointerException

jkhazraji

Active Member
Licensed User
Longtime User
I have this type declaration in Process_Global:
B4X:
    Type tempbox(X(4) As Int,Y(4) As Int ,r As Int, g As Int, b As Int, rot As Int)
    Dim Box(8) As tempbox
When I try to fill some of the type members with values, in Start_Activity or in any other sub , as in the following:
B4X:
    Box(1).rot = 4
    Box(2).rot = 1
    For i = 3 To 6
        Box(i).rot = 4
    Next
    Box(7).rot = 2
or the following in a Timer after being initialized and enabled:
B4X:
        For i = 1 To 4
            Box(rnum).Y(i) =  Box(rnum).Y(i) + boxwidth
        Next
I will get the following error:
(NullPointerException) java.lang.NullPointerException

Any help to fix it?
 

udg

Expert
Licensed User
Longtime User
Did you initialize the tempobox items in the array?
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
Thanks a lot.. but what about this error?
B4X:
    Dim tempx(4) As Int 'in Process_Globals
.
.
.
sub init(i as int)
    For i = 1 To 4
        box(rnum).X(i) = tempx(i)
        box(rnum).Y(i) = tempy(i)
    Next 
end sub
will give an error of: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Arrays have index starting at 0
So Dim tempx(4) leads to indexes 0,1,2,3 while your For loop counts from 1 to 4. That causes the Index Out Of Bounds error.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…