Passing a nul pointer in on_mouse_click() ?

Started by bx83, Sat 19/12/2020 04:51:02

Previous topic - Next topic

bx83

I've had some trouble with the on_mouse_click() function recently.
This error is runtime and intermittent - it's happened twice so far.

The rules for mouse clicks logic in the inventory window are:

Right click
  Are you over a non-null item (ie. you're not clicking on an empty area)?
    Can this item have a Useinv interaction run on it?
      If so, load the item you just clicked on as the activeinventory item.

Left click
  Is there an item loaded as the activeinventory item?
    Yes - combine the activeinventory item with the object you just clicked on (ie. activeinventory=key; useinv key with door)
    No - LOOK at the inventory object you just clicked on
    if the inventory item you just clicked wasn't there (ie. you clicked an empty area), then do nothing.

Here's the code:

Code: ags
...

InventoryItem *tInv;
tInv = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
    
    if (button == eMouseRightInv) {
      if (tInv!=null && tInv.IsInteractionAvailable(eModeUseinv)) {
        player.ActiveInventory=inventory[game.inv_activated];
        player.ActiveInventory.CursorGraphic=inventory[game.inv_activated].CursorGraphic;
      }
    }
   
if (button == eMouseLeftInv) {
  if (player.ActiveInventory) {
    if (tInv!=null) {
      tInv.RunInteraction(eModeUseinv);
      mouse.Mode=eModeLookat;
      RemoveActiveInventory=true;
    }
} else {
    mouse.Mode=eModeLookat;
    player.ActiveInventory=null;
    tInv.RunInteraction(eModeLookat);
  }
}

...


The error is: null pointer referenced.

My thoughts are that it has something to do with line 4: tInv is defined. Because the position is defined a second before the if statement catches it, I think that the rare situation when it references a null pointer has something to do with the user moving the mouse sharply, and clicking on an item when they were just over a point that was empty?.. ie. they pass a null pointer, when they thought they were clicking on an actual item?.......  No idea to be honest :/

Can anyone see any obvious mistakes, improper code, or the runtime error itself? It's definitely in the above logic, just no idea where :/

Crimson Wizard

#1
Quote from: bx83 on Sat 19/12/2020 04:51:02
The error is: null pointer referenced.

When this happens in AGS it usually tells exact line this happened and then you may guess at least to which pointer it refers to. Could you clarify this (and maybe post full error message)?


NOTE: you do not have tInv null check in the last block, where you do tInv.RunInteraction(eModeLookat);


Quote from: bx83 on Sat 19/12/2020 04:51:02
My thoughts are that it has something to do with line 4: tInv is defined. Because the position is defined a second before the if statement catches it, I think that the rare situation when it references a null pointer has something to do with the user moving the mouse sharply, and clicking on an item when they were just over a point that was empty?.. ie. they pass a null pointer, when they thought they were clicking on an actual item?

It does not matter how player moved the mouse: so long as tInv is assigned once in this code it will remain assigned to same value until function ends, and all its checks will be against that value.

bx83

Could you elaborate on what you were saying in ‘note:’? I think that might be the one.
And the line number of the error was:

if (button == eMouseLeftInv) {
  if (player.ActiveInventory) {
    if (tInv!=null) {
      tInv.RunInteraction(eModeUseinv);
      mouse.Mode=eModeLookat;
      RemoveActiveInventory=true;
    } <=== THIS LINE WAS THE NULL POINTER ERROR
} else {

Crimson Wizard

Quote from: bx83 on Sat 19/12/2020 12:12:37
Could you elaborate on what you were saying in ‘note:’? I think that might be the one.

There's a last block in your posted script:
Code: ags

  } else {
    mouse.Mode=eModeLookat;
    player.ActiveInventory=null;
    tInv.RunInteraction(eModeLookat);
  }


You are calling tInv.RunInteraction(eModeLookat) without checking if tInv is null or not.


The error line location is quite strange indeed, maybe something is off in compiler report.

bx83

#4
I thought it meant the block within the open/close-curly-bracket, or perhaps just the previous line, was generating the error?

Anyway fixed the checking of tInv before lookat action, this was possibly the area the error was referring to. Thank you :)

SMF spam blocked by CleanTalk