# I keep getting a null pointer exception – sometimes

I have written a few methods to spawn equipment that you find in certain places around the map in my game. It may not be the best way to do it but it works - sometimes. Sometimes I just get a null pointer exception on a spatial and I cant figure out why. Here is how I assign a model (or a box, in place of a model) to an array of spatials:

gear[values] = makeCube();

gear[values] = makeCube();

gear[values] = makeCube();

gear[values] = makeCube();

gear[values] = makeCube();

gear[values] = makeCube();[/java]

And here is how I utilize this spatial array:

[java]private void Spawn(Vector3f[] points){

Spatial[] gear = makeGear(points);

for(int i=0;i<=9;i++){

gear.setLocalTranslation(points);

rootNode.attachChild(gear);

}

}[/java]

And i get a nullpointer sometimes on

gear.setLocalTranslation(points);

gear[values] → where values is unknown integer for me(from unknown - unknown) where i is integer(from 0 - 9)

and

gear → where i is integer(from 0 - 9)

i see difference here …

yes and a stack trace, what is “i” when the NPE occurs?

Right, values is

int[] values = new int

and is filled with random values from one - 10, so the number 1 will only occur once, the number 2 will only occur once.

e.g 2,7,9,1,6,3,5,8,4,0

This is to allow the gear to be spawned in a different ‘loot point’ every time. I can show you how I make values[] in this way but you might not like it.

[java]private Spatial[] makeGear(Vector3f[] points){

Spatial[] gear = new Spatial;

int[] values = new int;

values = rand(10);

values = rand(10);

while(values == values){

values = rand(10);

}

values = rand(10);

while(values == values || values == values){

values = rand(10);

}

values = rand(10);

while(values == values || values == values || values == values){

values = rand(10);

}

values = rand(10);

while(values == values || values == values || values == values || values == values){

values = rand(10);

}

values = rand(10);

while(values == values || values == values || values == values || values == values || values == values){

values = rand(10);

}

values = rand(10);

while(values == values || values == values || values == values || values == values || values == values || values == values){

values = rand(10);

}

values = rand(10);

while(values == values || values == values || values == values || values == values || values == values || values == values || values == values){

values = rand(10);

}

values = rand(10);

while(values == values || values == values || values == values || values == values || values == values || values == values || values == values || values == values){

values = rand(10);

}

values = rand(10);

while(values == values || values == values || values == values || values == values || values == values || values == values || values == values || values == values || values == values){

values = rand(10);

}

gear[values] = makeCube();

gear[values] = makeCube();

gear[values] = makeCube();

gear[values] = makeCube();

gear[values] = makeCube();

gear[values] = makeCube();

return gear;

}[/java]

Rand method just calls Random.nextInt, but I can put min and max caps on it

omg, do you want to write in Asembler [java]Collections.shuffle(list, new Random(seed));[/java]

if im not wrong, it should do what you need.

just try to write in “higher” level. that was java created to WTF I did all that for nothing??

I found the problem anyways,

while(values == values || values == values || values == values || values == values || values == values || values == values || values == values){

values = rand(10);

It was that line, thanks though ill try this

also never base a random algorithm on using while, the performance will not be consistent and you could theoretically be there forever, especially ones near the bottom.

Edit: and you will also make mistakes like that and your procedure does not scale well, try it with 1000 items, and you’ll see what i mean

there are many examples like here:

Collections: shuffle(List < ? > list,Random rnd) : Collections « java.util « Java by API

Collections: swap(List<?> list, int i, int j) : Collections « java.util « Java by API

Collections: reverseOrder() : Collections « java.util « Java by API

or even Arrays can do something(sorting here, but randomize maybe too, idk):

http://www.leepoint.net/notes-java/data/arrays/70sorting.html

the only you need is to use Java Collections.

@wezrule said:
also never base a random algorithm on using while, the performance will not be consistent and you could theoretically be there forever, especially ones near the bottom.

Edit: and you will also make mistakes like that ;) and your procedure does not scale well, try it with 1000 items, and you'll see what i mean

Hell no 10 of them took me long enough.

I'll have a look thanks oxplay
1 Like

But I can’t use gear[myList] = assetmanager.blahblahblah… can I?

you should make Collection, for example ArrayList, and put your Spatials there.

after that you can make random swap / shuffle on it.

but at all you dont need use Collections, you can use Array and use:

[java]Arrays.sort(employee, new AgeComparator());[/java]

look example:

where you can decide on elements positions (so you can even here random their positions)

this comparator can be used on Arrays and Collections like ArrayList / etc

Yay, made my own ArrayList up and named it listy <3 you have made me very happy, thank you!

Comparator: not sure I’m ready for that yet… I can do without it anyway @javagame said:
WTF I did all that for nothing??

Yep. If you find yourself doing that sort of code then you always need to step back and think about whether there is a smarter way to do it - or whether someone else has already solved the problem. Especially considering that things like shuffling a list/array are a very common requirement.