Android Question Accessing Main Globals panel data in another code module

Discussion in 'Android Questions' started by Vandermyer, Jul 10, 2015.

  1. Vandermyer

    Vandermyer New Member Licensed User

    Within Globals (in Main) a Public Panel is defined. The Activity_Create Sub performs some initialisation that includes calling a Sub in a code module that uses this Panel (accessed via Main.pnlMain). I find that sometimes when trying to access Main.pnlMain I get a exception, and sometimes I don't. The code looks something like this (slightly edited to removed code that does not use pnlMain):

    Sub Globals

    'These global variables will be redeclared each time the activity is created.

    'These variables can only be accessed from this module.

    Public pnlMain As Panel

    End Sub

    Sub Activity_Create(FirstTime As Boolean)


    pnlMain.Width = 

    pnlMain.Height = 


    End Sub

    Sub Initialise()

    Dim myds As Rect

    myds.Initialize(xpos, Main.pnlMain.Height - 
    10%y, xpos + 6%x, Main.pnlMain.Height)

    End Sub
    1) If you look at the screen image 'Main null' it can be seen that hovering on Main in debug mode reveals that Main is (apparently) null, but data is displayed for Main in the watch window and it can also be seen that the line with the breakpoint has been successfully stepped over.
    2) In the image 'Exception' a null object reference has occurred.

    Why does this happen? What am I missing? It seemed to me (although this is a bit subjective) that the null exception tended to occur more when the Initialise sub was called earlier the Activity_Create and when the phone's orientation changed.

    Any thoughts or ideas appreciated!

    Attached Files:

  2. mark35at

    mark35at Well-Known Member Licensed User

    If I understand you correctly, you have made a small mistake:

    Main.pnlMain can only be called outside of the activity main when the pnlMain is declared in Process Globals not Globals. Globals means only for this activity.
  3. Vandermyer

    Vandermyer New Member Licensed User

    Thanks for the fast reply.

    Main,pnlMain is a Panel, so cannot be defined in Process_Globals - doing so will give the error:

    Error description: Cannot access activity object from sub Process_Globals.
  4. klaus

    klaus Expert Licensed User

    You cannot access an Activity object directly from outsides the modules where it is declared.
    In a code module you can pass objects in the calling routine.
    In your case:
    Sub Initialize(pnl As Panel)
    myds.Initialize(xpos, pnl.Height - 10%y, xpos + 6%x, pnl.Height)
    RandomCoder likes this.
  5. Vandermyer

    Vandermyer New Member Licensed User

    Thanks. That has resolved the issue.

    It would be nice if the compiler could enforce the restriction that an Activity variable cannot be accessed outside the module where it is declared.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice