- Home /
Generic List in static function
Hi, I'm working with static functions, and in this sample code there is my test class.
// SimpleClass.js
#pragma strict
import System.Collections.Generic;
static var randomnumbers:List.<int>;
static var randomnumber:int;
static function RandomOne()
{
randomnumber = Random.Range(0,11);
return randomnumber;
}
static function RandomList(howmany:int)
{
for (var i = 0;i<howmany;i++)
{
var tempvalue:int = Random.Range(0,11);
randomnumbers.Add(tempvalue);
}
return randomnumbers;
}
Now, when I call the first method, the one returning a random int, no problems
number = SimpleClass.RandomOne();
the problems shows up when I try to call the second method, the one returning a populated list of random ints
somerandomnumbers = SimpleClass.RandomList(5);
it yields "NullReferenceException: Object reference not set to an instance of an object" upon trying to add an element to the static array. Don't get me wrong, I don't really need that array to be static, it's just that as far as i can see, static methods, can only deal with static variables. And what i'm trying to achieve, is pretty much having a collection of static functions without being carried by an actual gameobject. (so delightfully calling them MyTools.someTools(something,something);
thank you ! :)
I think there are some code missing ... And a question :)
actually none of them should be missing :) i try to carefully read before posting. anyway, thanks for the attention, and here's again the question:
Is there any limitation/special case when working with Lists, when you make them static, [to use them in a static function (to be able to call it likewise in the two lines you refer to as missing code)]
thank you again.
Oh but of course you probably refer to my previous attempt at editing, gone horribly wrong :)
Answer by Eric5h5 · Jun 22, 2013 at 06:27 PM
You haven't initialized the randomnumbers list anywhere, hence the null reference error. If you want to return a unique list every time the RandomList function is called, however, the randomnumbers variable should not be a global static variable, instead it should be a local variable declared inside the RandomList function. Same goes for the RandomOne function; there's no reason for it to return anything if you're using a global static variable.
Here's what I was waiting for :) Thanks Eric, now it's clear.
But what would you suggest, since inside my implementation of a pathfinding script, I need a global(for every pathfinding request) List inside of my pathfinding method as a [queue for recursive calling][1]
Thank you ! [1]: http://en.wikipedia.org/wiki/Pathfinding#Sample_algorithm
In that case it's O$$anonymous$$ to leave things as they are, though for the sake of correctness, the variables should be made private, since you're only accessing them through the functions.
Thanks !
i'm inizializing randomnumbers this way
static private var randomnumbers:List.<int>;
as a global variable inside my class, right after randomnumber:int; and it still still says "Object reference not set to an instance of an object" am I initializing it wrong ?
never$$anonymous$$d, i wasn't instancing it properly, will keep updated my progress in understanding the original topic of the question :)
Right, that's just declaring the variable, not initializing it.