Null Pointer Referenced Problem (SOLVED)

Started by mode7, Fri 17/09/2010 19:54:12

Previous topic - Next topic

mode7

Hey everyone,
I hope someone can help me here because my head is going to explode if I think about the problem any longer.
My coding style is probably horrible, because I'm not really experienced with coding.

CODE:

function room_RepExec()
{
if (goInv.ItemCount > 0) item = goInv.ItemAtIndex[goInv.TopItem].Name;
}

...

function region5_Standing()
{
if  ((item == "iKerze") && (useinv == 1)) //useinv determines if the inventory key is pressed
  {
     useinv = 0;
     // RANDOM STUFF HAPPENING
     cEgo.LoseInventory(iKerze);
    }
}


HOW IT'S SUPPOSED TO WORK:

When you press a certain key the player should use the currently displayed inventory item, which is the "TopItem". So I use my custom "item" String to store the name of the "TopItem", in the RepExec function.
It works with two exceptions: If you don't have any items, the "goInv.ItemAtIndex[goInv.TopItem].Name" will return NULL which I already prevented from happening.

But if the TopItem is the last item in the inventory i get the NULL POINTER error again after I lose the Item. If the item I use is not the last item everything works fine.


And this is what I don't understand. When I do "LooseInventory" the "TopItem" should by lower by 1 and the next item should be selected. Or am I getting this wrong?

I hope someone here can explain and possible help me to fix this.

Thanks in advance
- mode7

Calin Leafshade

if you use a pointer you can actually check if a null pointer has been referenced

Code: ags

function room_RepExec()
{
InventoryItem* invitem = goInv.ItemAtIndex[goInv.TopItem];
}


then you can do whatever you like with this item including checking whether or not it is null before you try to do anything with it.

Code: ags

if (invitem != null){
// etc
}



you can also reference pointers directly:

Code: ags

player.LoseInventory(invitem);

mode7

#2
Thanks a lot Calin for your quick response,
well I don't know anything about pointers and the help file didn't help much. So I'm kind of in the dark here.

I used you first line and then:

function region5_Standing()
{
if  ((invitem == iKerze) && (useinv == 1)) //useinv determines if the inventory key is pressed
   {
      useinv = 0;
      // RANDOM STUFF HAPPENING
      cEgo.LoseInventory(invitem);
     }
}

I get an undefined symbol error. However I don't get the error if I use "invitem" within the repExec function.

I know it's a completely different error which has to do with my total ignorance of pointers, but I can't check if your fix is working until I figure it out.


EDIT: Got it Working!! Had to define the pointer at the beginning of the script because I wanted to use it outside of the function (BTW,this was very useful: http://americangirlscouts.org/agswiki/AGS_Pointers_for_Dummies).

This is how I did it:

InventoryItem *item;
...
function room_RepExec()
{
item = goInv.ItemAtIndex[goInv.TopItem];
}

...

function region5_Standing()
{
if  ((item == iKerze) && (useinv == 1)) //useinv determines if the inventory key is pressed
   {
      useinv = 0;
      // RANDOM STUFF HAPPENING
      cEgo.LoseInventory(invitem);
     }
}

The funny thing is, I don't get any Null Pointer errors even when I should get them, like in the beginning when I don't have items.
Anyway thank you very much Calin!!

tzachs

Where do you define invitem?
If you define the variable inside the repeatedly execute function then the scope of the variable will be inside that function, meaning you won't be able to access it from other functions and you'll get the undefined symbol error.

If the region5_Standing function is in the same file as the repeatedly execute, you should define the variable to be outside the functions (at the beginning of the file, it should be declared above both of the functions).

SMF spam blocked by CleanTalk