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:
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?
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);
}
}
}
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.
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);
}
}
}
That's right, don't know why I wrote it like that, weird. :P
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".
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
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).