"Null pointer referenced" in function call (SOLVED)

Started by TheMagician, Fri 28/07/2006 01:42:53

Previous topic - Next topic

TheMagician

Hi everybody!

I have a Module called FireFly with this simple header:
Code: ags

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



So, this is part my RoomScript:
Code: ags

//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:
Code: ags

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

Khris


Gilbert

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).

Alynn

In your header:
Code: ags
struct FireFly {
  import static function CreateFlies();
  //in repeatedly execute
  import static function UpdateFlies();
};


And in your script file
Code: ags
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.


TheMagician

Well thanks everybody. I followed your advices but they didn't solve the problem.

However, I just found out what caused the error:
Code: ags

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:
Code: ags

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

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


Isn't that allowed?

Gilbert

That should be okay, are you sure that i wasn't also declared outside of the functions?

TheMagician

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

Kweepa

Hum...
Shouldn't you be initializing 'i'?
Code: ags

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

Still waiting for Purity of the Surf II


SMF spam blocked by CleanTalk