Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: mode7 on Mon 09/01/2012 20:34:14

Title: Null Pointer exception when accessing local struct via global function [SOLVED]
Post by: mode7 on Mon 09/01/2012 20:34:14
Hey guys, this one is rather hard to explain.
I have two module scripts

In the first module script have a private struct that manages all the operations called _rectangle
I also have the public (imported) struct called Rect which has some functions and properties used to read and write the significant variables to the local structs

For example i can call

Rect.GetX(ID)

from the Global script to get the x-coordinates of the given id

The same call will throw a null pointer error if i try to access it via my second module script.
The second module script has a public struct ("Rectangle") which contains the function "Move"
When called in the global script via

Rectangle.Move(ID, ..other parameters)

...it internally trys to get the coordintes via the Rect.GetX(ID) function which throws the exception
Even though the pointers seem okay from the globalscript's point of view.

Passing through the coordinates via parameters won't help because I also need to write data with a function in the same method, which also will gives a null pointer

So accessing any data structures of Rect via a member of the Rectangle struct wont work - why?
Title: Re: Null Pointer exception when accessing local struct via global function
Post by: Khris on Mon 09/01/2012 22:53:18
A null pointer error occurs when the part before the dot isn't pointing at an instance.

I'm guessing you've misplaced the import and export lines and/or declarations.
It's pretty much impossible to say anything for sure without seeing the actual code you're using.

If I had to guess I'd say you declared the struct variable in the header, thereby creating separate, independent instances. Thus referencing Rect in Script A is fine, but Script B's Rect is still pointing at null.
Title: Re: Null Pointer exception when accessing local struct via global function
Post by: mode7 on Mon 09/01/2012 23:18:15
Hey Khris, thanks for the answer. I think you found the problem. So what would be the right thing to do. Declaring it in the script (instead of the header?) exporting and importing it again in the header?
Title: Re: Null Pointer exception when accessing local struct via global function
Post by: Khris on Mon 09/01/2012 23:28:56
In general, never declare any variables in the header.
The header is exclusively for defines (constants), enums, struct definitions and imports lines.

The usual way is this:

// header

struct _rectangle {
  ...
};

import _rectangle Rect;


// main script

_rectangle Rect;
export Rect;


Note that instead of importing in the same header, you can do it on a per script basis if only some scripts need access. It is safe to import at the top of the script. If you import something in a header, the corresponding and every subsequent script can access the data.
Title: Re: Null Pointer exception when accessing local struct via global function
Post by: mode7 on Mon 09/01/2012 23:37:02
Thanks alot Khris, this is not only the solution to my problem but also very useful information. Thanks again!!