Weird NullPointerExeption I cant track down.

I’m trying to get a rudimentory particel engine built for y game project.

I have a number of different classes that all eventualy add their particle effect to a publicaly available ArrayList (the global effect list) in my Game class that stores them for rendering and update.

There are a few times that for some reason I cant explain, where I get a NullPointerExeption when adding to this list, even after it has already been sucsessfully accessed, written to and read from with no problem.

The error apears to be happening only when my ParticleEmitterComponent trys to add its effect to the global effect list, at which point the NullPointerException occours. I think this means that the list itself is equle to null - but I’m not sure if that is what this error means.

If anyone can help or explain to me exactly what this error means, it would be much apreceated.

Below is the quoted code from my project journal thread.

This looks like a threading problem to me. The whole of Awt and Swing are not threadsafe and are full
of problems like this, even without the your extensions. There are always several threads actively
traversing the window hierarchy; you have (at least) to be very careful; but a better design is to minimize
your exposure by not making actively changing components part of the window hierarchy.

Paste this line of code:

ParticleEmitterComponent.java:32

The only line in the constructor that can cause a null pointer is

Main.getGame().GetParticleEngine().AddEffect(myEffect);

So either

Main.getGame()

or

.GetParticleEngine()

is returning null. Split the calls onto different lines to find out which it is.

ahh, cool, thanks guys.

I managed to fix it by making the ParticleEngine a static class. I’m not really happy with that, but it works and there will only be one ParticleEngine in the game ever anyway, so its not a disaster :stuck_out_tongue:

I has concluded that also Bleb, but both of those were already instanceated and doing stuff before the error occours…

What exactly does that line of code do Demonpants (or what does it even mean - I’ve not come across that syntax before), and where to paste it?

chears again for yout help.

ParticleEmitterComponent.java:32

means the exception is thrown on line 32 of that file

Yes, and this is very very important to know. The compiler is telling you that you had a null pointer exception exactly where it happened (in ParticleEmitterComponent on line 32). Find that line of code, then find which object being referenced is null, and you know the problem.

If the line of code is as simple as this:


String output = object.toString();

then you would know that object would have to be null, because it’s the only reference happening on that line.

If it is more complicated, like this:


String output = object.getProperty().getComponent().toString();

then you know that either object is null, or its property is null, or its property’s component is null. Obviously this case is more complicated, so to solve it you would split that line up like Bleb mentioned:


Property prop = object.getProperty();
Component comp = prop.getComponent();
String output = comp.toString();

Then the compiler will tell you which line has the exception, and you know exactly which thing is null. Alternatively, you can do this:


System.out.println(object);
System.out.println(object.getProperty());
System.out.println(object.getProperty().getComponent());

Which might give you output of


<Object:GHDFGH>
<Property:HVJHVA>
null

In which case you would know the component was null, or you could get


<Object:DKFJGHSFJK>
null
NullPointerException

Either way, you’ll know what’s null.

Make sense. NullPointerExceptions are by far the most common runtime errors that new Java programmers see, but they’re also one of the easiest to fix.

Cool, thanks for the masterclass :slight_smile:

I get them from time to time, and generaly they’re easy enough to track down. This one just really confused me. I hadn’t thought of breaking the line apart like that to find it.

Thanks all

Easier to just use a breakpoint.

I tryed that, but i could not find a way to see which part of the line was null. When running a breakpoint, only veriable-names seem to have and mouse-over info (in NetBeans).

That said, theres probably quite a few books worth of stuff that I dont know about debugging in netbeans.

If you used a breakpoint, you’d set it the line before, and then step by step allow it to perform each function. Eventually one of them would have a null value.