Problem using global var

Spacewalker

Member
Licensed User
Longtime User
Hello,

I have a problem using global variables.
I have 2 modules like this:

Modul "Main": This module is used to enter some data like "size" :
B4X:
sub Process_Globals
dim iSize as int      '**needs to be a global var (main.iSize)
end sub

sub myButton_Click
iSize = myEditTextSize.text
StartActivity ("Game") 
end sub
Activity-Modul "Game": In this module the variables set in "Main" must be processed
B4X:
Sub process_globals
dim iCalcNewSize as int   '**needs to be a global var (Game.iCalcNewSize)

iCalcNewSize = main.iSize * 10         <---crash here
end sub
I can compile the code but on run it crashes immediately. The log says:
LogCat connected to: emulator-5554
process_globals start
maininitializeProcessGlobals (java line: 293)
java.lang.RuntimeException: java.lang.NullPointerException
at ss.com.buttons.main.initializeProcessGlobals(main.java:293)
at ss.com.buttons.main.afterFirstLayout(main.java:80)
at ss.com.buttons.main.access$100(main.java:16)
at ss.com.buttons.main$WaitForLayout.run(main.java:72)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at ss.com.buttons.Game._process_globals(Game.java:888)
at ss.com.buttons.main.initializeProcessGlobals(main.java:287)
... 12 more


I can use the variables in sub Globals, but this doesnt help me because I need declare iCalcNewSize as global.

Heinz
 

Spacewalker

Member
Licensed User
Longtime User
Try to move this line to Activity_Create.
B4X:
iCalcNewSize = main.iSize * 10
Best regards.

Thank you for you answer.

What you have suggested would work I think, but not for me:
My example was not complete... I forgot a line, so here again the complete code:

B4X:
Modul "Main": This module is used to enter some data like "size" :
sub Process_Globals
dim iSize as int      '**needs to be a global var (main.iSize)
end sub
   
sub myButton_Click
iSize = myEditTextSize.text
StartActivity ("Game") 
end sub
Activity-Modul "Game": In this module the variables set in "Main" must be processed
B4X:
Sub process_globals
dim iCalcNewSize as int   '**needs to be a global var (Game.iCalcNewSize)

iCalcNewSize = main.iSize * 10         <---crash here
dim theArray(iCalcNewSize)  as int     <---this array needs to be a global var, therefore I can not move the DIM to activity_create...?

end sub
So if I move iCalcNewSize = main.iSize * 10 to activity_create as you suggested then I can not DIM TheArray(iCalcNewSize) as global...right ?


Heinz
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
If you set the iCalcNewSize in the previous module and then declare theArray in your next process_globals as dim theArray(main.iCalcNewSize) will it still crash? I haven't checked it, just a thought. Also, you can always dim a list, which is dynamic and you can avoid the need of the upper limit.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…