Null error WillShow event of Window

  1. last year

    Marc

    15 Feb 2019 User since 2018

    I followed the Todo tutorial and got a null error in the WillShow event, for this code:

    TableView1.Header.TextField1.text = ""

    If I use the same code in DidShow, then there's no problem.

  2. andrea

    15 Feb 2019 Administrator User since 2016
    Edited last year by andrea

    Hi @Marc,
    You get the "Null Error" because in the WillShow event and not in the DidShow event because at that time (WillShow) the TableView1 object has already been initialized, but the content of the TableView1 (for example Header) has not yet been initialized.
    The initialization of an object is deferred until the point at which it is needed (lazy loading), so the subnodes of each object are normally loaded just before the WillShow event.
    This kind of errors (Null Error: messages sent to null objects) is silently ignored by default, you are receiving this error because you enabled this particular logging in the Advanced Preferences (Menu - Creo - Preferences... - Advanced tab).
    In this case you could safely ignore this error, the meaning of that code from the tutorial is to clean the content of that TextField before showing again the Window2:

    • the first time the Window2 is opened the content of the TextField1 doesn't need to be cleaned and it is not a problem if the TextField1 doesn't exist at that moment
    • after the first visualization of the Window2, if the user goes back to Window1 and then opens another time the Window2, this time the TextField1 is already loaded and the TableView1.Header.TextField1.text = "" will work as expected. For this scenario, it is better to call it in the WillShow than in the DidShow event because otherwise the user could see the old content of the TextField1 during the presentation of the Window2.

    To avoid the error to be fired, you could disable that advanced option or you can improve that code by checking for the availability of the Header object with:
    if (TableView1.Header) TableView1.Header.TextField1.text = ""

or Sign Up to reply!