Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: EliasFrost on Fri 20/06/2014 12:42:15

Title: Null pointer referenced - GUI Click
Post by: EliasFrost on Fri 20/06/2014 12:42:15
One of my testers get an error saying that a null pointer was referenced at line 270 in the global script which is part of this snipped:

Code (ags) Select
void HandleGUIClicks(GUIControl *gc, MouseButton button) {
  if (button != eMouseLeft) return;
  if (gc.AsButton != null) Buttons(gc.AsButton); // <-- this line right here --
  if (gc.AsButton != null)
  {
    j_buttons(gc.AsButton);
  }
  if (gc.AsButton != null)
  {
    m_buttons(gc.AsButton);
  }
}


I would understand if I did reference a null pointer just like that but with the script I check beforehand if gc.AsButton is null or not, which should prevent the script from calling Buttons(); if it is null. I can't reproduce the bug myself and none of my other testers have this problem, could a more experience scripter take a look at it please and share your thoughts on what may be causing this problem?
Title: Re: Null pointer referenced - GUI Click
Post by: Joe on Fri 20/06/2014 12:49:39
Maybe your tester is clicking an empty area so the GUIControl (gc) is null.
Try this:


    void HandleGUIClicks(GUIControl *gc, MouseButton button) {
      if(gc!=null){//just add this condition
        if (button != eMouseLeft) return;
        if (gc.AsButton != null) Buttons(gc.AsButton); // <-- this line right here --
        if (gc.AsButton != null)
        {
          j_buttons(gc.AsButton);
        }
        if (gc.AsButton != null)
        {
          m_buttons(gc.AsButton);
        }
      }
    }
Title: Re: Null pointer referenced - GUI Click
Post by: EliasFrost on Fri 20/06/2014 13:00:49
I tried to reproduce it by clicking outside the gui, empty gui spaces and click-drag from a button to an empty space and vice versa without the error popping up. I'm still not entirely sure what the problem is but your suggestion is a lot safer than the script was before so I'll use it and keep that in mind, thanks.

EDIT: I'll update this post after I've sent the new version to the tester.
Title: Re: Null pointer referenced - GUI Click
Post by: Joe on Fri 20/06/2014 17:59:18
Good. Also mention that you can save some IFs unless I'm missing something:


    void HandleGUIClicks(GUIControl *gc, MouseButton button) {
      if(gc!=null){//just add this condition
        if (button != eMouseLeft) return;
        if (gc.AsButton != null)
        {
          Buttons(gc.AsButton);
          j_buttons(gc.AsButton);
          m_buttons(gc.AsButton);
        }
      }
    }
Title: Re: Null pointer referenced - GUI Click
Post by: EliasFrost on Fri 20/06/2014 18:42:31
That's right, don't know why I wrote it like that, weird. :P
Title: Re: Null pointer referenced - GUI Click
Post by: EliasFrost on Tue 24/06/2014 11:51:41
It seemed that the problem fixed itself somehow, now the old version works just fine, and so does the new one. Any idea why this sort of thing happens? I've seen it several times before on these boards that similar problems with null pointers arise and then "fixes itself".
Title: Re: Null pointer referenced - GUI Click
Post by: Calin Leafshade on Tue 24/06/2014 17:56:49
I think gc is likely null because you get the GUI using the mouse coordinates after registering a click.

This sometimes means that the mouse can be over the control when the click occurs but may have moved before you actually get the control using the mouse coords and so you can register a gui click but have a null control.

To know for sure we'd need to see what calls your function
Title: Re: Null pointer referenced - GUI Click
Post by: Crimson Wizard on Tue 24/06/2014 18:06:34
Quote from: EliasFrost on Tue 24/06/2014 11:51:41
It seemed that the problem fixed itself somehow, now the old version works just fine, and so does the new one. Any idea why this sort of thing happens? I've seen it several times before on these boards that similar problems with null pointers arise and then "fixes itself".
Programs do not fix themselves, ever.
Such situation usually means that either problem was fixed occasionally by some seemingly unrelated change in script, or that error occurs under conditions that are difficult to replicate (and the problem is still there).