Android Question [ANSWERED] IsInitialized() or <> Null

Discussion in 'Android Questions' started by Lee Gillie CCP, Jun 23, 2015.

  1. Lee Gillie CCP

    Lee Gillie CCP Active Member Licensed User

    I don't understand the precise between the IsInitialized function and testing for <> Null.

    If I try to strictly use IsInitialized and have a pattern of calling Initialize to what I mentally equate to creating a new object and invoking a constructor. And at the end of life to set the variable to Null. When I then do a subsequent IsInitialized it gives a null pointer exception.

    Calling Initialize on an object, and testing IsInitialized seem contrary to my experiences in other languages because "New" has not been invoked. Based on observation, apparently B4A objects are implicitly created but are "not initialized"?? Once Initialize is called I don't know how to revert the object to its previous state, so that I can again call IsInitialized without the null pointer error.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    When you call:
    Code:
    Dim o As Object
    o already references an object. Depending on the type of object it may be initialized or not (not all objects need to be initialized). In most cases you will not encounter Null references.

    You do not need to revert it to its previous state. Once the variable scope ends the object will be released (if there are no other references to the object).

    Some methods might return null.
    In such cases it is safest to check for:
    Code:
    If Map1 <> Null AND Map.IsInitialized Then
     
  3. Lee Gillie CCP

    Lee Gillie CCP Active Member Licensed User

    Ok, I get it. Once the Dim o as Object has instanced the object, and you set it to Null, you can not get back to an instanced and uninitialized object.

    I was attempting to use the instantiated object as a sort of flag for the activity it manages (in my case drag sorting items in a list) that the drag was active, and when they let go, I would set the drag management object to Null, attempting to indicate for event handlers. But then I could never drag again for a second time. I was attempting to call Initialize to get it going again. It seems clear to me now to leave the object instantiated, and to have a separate boolean flag to indicate the drag is active.

    Understanding this pattern I am sure is very basic to the language, and known by all, and I just didn't quite get it. Discussion of this not in any of the books or references. Thank you so much for clarifying for me.
     
Loading...