Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: TheMagician on Fri 28/07/2006 01:42:53

Title: "Null pointer referenced" in function call (SOLVED)
Post by: TheMagician on Fri 28/07/2006 01:42:53
Hi everybody!

I have a Module called FireFly with this simple header:

struct FireFly {
  import function CreateFlies();
  //in repeatedly execute
  import function UpdateFlies();
};



So, this is part my RoomScript:

//Create instance of FireFly-Struct
FireFly thisroom;

#sectionstart room_b  // DO NOT EDIT OR REMOVE THIS LINE
function room_b() {
  // script for Room: Player enters room (before fadein)
 
  thisroom.CreateFlies();    //<===== !!!!! THIS LINE CAUSES THE ERROR !!!!!
}
#sectionend room_b  // DO NOT EDIT OR REMOVE THIS LINE


It refers to this section of my FireFly-Module:

function FireFly::CreateFlies() {
 
  Mouse.Visible = false;
  int i;
  while(i < 3) {
    //DoStuff
  }
}



AGS compiles fine. However, just when the room is about to load I get an error saying:
"Null pointer referenced" in the line I indicated.

Is that enough information for you to have any ideas what I did wrong?
Many thanks in advance,
Stefan
Title: Re: "Null pointer referenced" in function call
Post by: Khris on Fri 28/07/2006 02:52:05
Try "mouse.Visible=false;".
Title: Re: "Null pointer referenced" in function call
Post by: Gilbert on Fri 28/07/2006 03:40:33
Actually I'd tried both, seems that they both worked (the mouse. variables were left because of compatibility with old versions, if you read the manual for these entries you'll see they're referred inconsistently as both Mouse.Visible and mouse.Visible).
Title: Re: "Null pointer referenced" in function call
Post by: Alynn on Fri 28/07/2006 05:43:22
In your header:
struct FireFly {
  import static function CreateFlies();
  //in repeatedly execute
  import static function UpdateFlies();
};


And in your script file
static function FireFly::CreateFlies() {

Otherwise you need to export the struct in the module header, and import it in your global script... Either works, but it's just generally easier to make them static.

Title: Re: "Null pointer referenced" in function call
Post by: TheMagician on Fri 28/07/2006 08:25:23
Well thanks everybody. I followed your advices but they didn't solve the problem.

However, I just found out what caused the error:

static function FireFly::CreateFlies() {

  Mouse.Visible = false;
  int i;                    <=== !!!!! CHANGE THIS VARIABLE NAME TO 'y' !!!!!
  while(i < 3) {
    //DoStuff
  }
}


If I changed the variable name from 'i' to 'y' everything worked perfect.
I don't really get why this happened:
I declared variables with the same name 'i' in other functions of my module as well, but I always thought that you can have the same variable names across several functions as long as you create the variable within a function.

So in my opinion the following should be possible:

function Test_One() {
  int i;
  //DoStuff
}

function Test_Two() {
  int i;
  //DoMoreStuff
}


Isn't that allowed?
Title: Re: "Null pointer referenced" in function call
Post by: Gilbert on Fri 28/07/2006 08:45:43
That should be okay, are you sure that i wasn't also declared outside of the functions?
Title: Re: "Null pointer referenced" in function call
Post by: TheMagician on Fri 28/07/2006 13:49:04
Well, very very strange .... I just changed the name of the variable back to 'i' and it works like a charm now. I didn't change the script in any other way...

Anyway, thank you very much for your input!
See you next time :-)

Stefan
Title: Re: "Null pointer referenced" in function call (SOLVED)
Post by: Kweepa on Fri 28/07/2006 14:31:23
Hum...
Shouldn't you be initializing 'i'?

int i = 0;
while (i < 3) {
  // Do stuff
}

Title: Re: "Null pointer referenced" in function call (SOLVED)
Post by: strazer on Fri 28/07/2006 14:51:27
Local variables are initialized to 0 by default (http://www.adventuregamestudio.co.uk/tracker.php?action=detail&id=271) as of AGS v2.70.