Arrays in Code Modules

mln

Member
Licensed User
Longtime User
I am new to Basic4Android (I am more of a C / C++ guy) so I figure I am just doing something silly here.

I am having trouble with arrays in a code module I have created. I define an array of double in the Process_Globals sub

'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals:

'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.


Dim tTokenType(25) As Int
Dim pTokenList(25) As Double

End Sub


Then I have a sub routine Init that I call to set things up in the module which I call from the main module:

Sub Init

Dim i As Int

For i = 0 To 24
pTokenList(i)=0.0
tTokenType(i) = 0
Next

End Sub

During debug, the program stops at pTokenList(i)=0.0 and I can see in the debugger under local variables, the error is LastException java.lang.NullPointerException

and under global variables, both tTokenType and pTokenList are null.

All non-array variables work as expected in the code module.

I have tried Dim pTokenList() as double in Process_Globals and then "re" dimming in init as a 25 piece array, but that just creates it as a localvariable.
I have also tried the same under Process_Globals without success.

Can someone point me in the right direction? Thanks in advance
 

margret

Well-Known Member
Licensed User
Longtime User
This code works fine:

Main
B4X:
'This code goes in the Main activity module
Sub Process_Globals
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean) 
   mycode.Init
   m = ""
   m1 = ""
   For t = 0 To 24
      m = m & mycode.pTokenList(t) & CRLF
      m1 = m1 & mycode.tTokenType(t) & CRLF
   Next
   Msgbox(m, "pTokenList Array")
   Msgbox(m1, "tTokenType Array")
End Sub

mycode
B4X:
'This code goes in a code module named mycode
Sub Process_Globals
   Dim tTokenType(25) As Int
   Dim pTokenList(25) As Double
End Sub
Sub Init
   For i = 0 To 24
      pTokenList(i) = 0.0
      tTokenType(i) = 0
   Next
End Sub
 
Last edited:
Upvote 0

mln

Member
Licensed User
Longtime User
Thanks for the response! Yep it worked when I ran your version so I figured I did something wrong somewhere else in my code.

Turns out sloppy coding on my side. I called a subroutine in the Main modules Process_Globals that called the Init function in my code module. So I figure the code modules globals are not defined until after the main modules. Oddly only the arrays caused me problems.

Thanks again!
 
Upvote 0
Top