Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Weird NullPointerExeption I cant track down.  (Read 1332 times)
0 Members and 1 Guest are viewing this topic.
Offline Dreamcatchermatt

Junior Member





« Posted 2010-05-23 14:16:31 »

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.


[code=Stack Trace]
run:
DRGame constructor - line 77.
Particle Engine Instanceated - line 98.
Load method begun - line 114.

Exception in thread "main" java.lang.NullPointerException
        at SpaceLib.Components.ParticleEmitterComponent.<init>(ParticleEmitterComponent.java:32)
        at SpaceLib.Objects.Projectile.<init>(Projectile.java:38)
        at SpaceLib.Components.WeaponComponent.initProjectile(WeaponComponent.java:102)
        at SpaceLib.Components.WeaponComponent.<init>(WeaponComponent.java:48)
        at SpaceLib.Ship.Ship.<init>(Ship.java:42)
        at GameLib.DRGame.Load(DRGame.java:130)
        at GameLib.DRGame.<init>(DRGame.java:105)
        at isogameengine_01.Main.main(Main.java:21)
[/code]

[code=ParticleEmitterComponent]public class ParticleEmitterComponent extends Component{

    ParticleEffect myEffect;

    public ParticleEmitterComponent(int id, Entity parent)
    {
        super(id, parent);

        this.myEffect = new ParticleEffect();
       
        Main.getGame().GetParticleEngine().AddEffect(myEffect);
    }

    @Override
    public void Update(int deltaTime)
    {
        this.myEffect.Position = this.Parent().GetPhysicsComponent().getPosition();
    }
}
[/code]

[code=Projectile]public class Projectile extends Entity{

    private Entity source;
    private double speed;
    private int projectileAge;
    private int projectileLife;

    private Vector2D destination;

    public Projectile(Entity source, Vector2D d)
    {
        this.source = source;
        this.destination = d;
        this.SetComponent(new PhysicsComponent(0, this));
        this.SetComponent(new RenderableComponent(0, this));
        this.SetComponent(new TrailComponent(0, this));
        this.SetComponent(new ParticleEmitterComponent(0, this));

        initVars();
        Fire();
    }
   
    private void initVars()
    {
        this.speed = 800;
        this.projectileAge = 0;
        this.projectileLife = 1000;
        this.GetRenderableComponent().GetSprite().load("artAssets\\projectiles\\cannonShell2.png");       
    }

    public void Fire()
    {
        Vector2D go = Vector2D.Sub(this.physicsComponent.getPosition(), destination);
        double theta = go.getAngle();

        this.physicsComponent.setPosition(this.source.GetPhysicsComponent().getPosition());
        this.physicsComponent.setVelocity(Vector2D.fromAngle(this.physicsComponent.getPosition(),
                speed,
                theta));
        //this.physicsComponent.setVelocity(new Vector2D(0,10));
    }

    private void Detonate()
    {
        /*
        if(this.GetParticleComponent() != null)
        {
            this.GetParticleComponent().AddEffect(this.GetPhysicsComponent().getPosition(),
                    Main.getEffects().Explosion1);
        }
        else
        {
            this.SetComponent(new ParticleComponent(0, this.parent));
            this.GetParticleComponent().AddEffect(this.GetPhysicsComponent().getPosition(), "Explosion1");
        }
       
         
        this.GetRenderableComponent().GetSprite().load("artAssets\\effects\\particles\\explosion1.png");
         *
         */
    }

    private void Expire()
    {
        this.Detonate();
    }

    public void Update(int deltaTime)
    {
        /*this.projectileAge += deltaTime;

        if(this.projectileAge > this.projectileLife)
        {
            //this.Expire();
            //this.parent.getChildren().remove(this);
        }
        */
            if(this.physicsComponent != null)
                this.physicsComponent.Update(deltaTime);

            if(this.renderableComponent != null)
                this.renderableComponent.Update(deltaTime);

            if(this.statsComponent != null)
                this.statsComponent.Update(deltaTime);

            if(this.navigatorComponent != null)
                this.navigatorComponent.Update(deltaTime);

            if(this.trailComponent != null)
                this.trailComponent.Update(deltaTime);

            if(this.particleEmitterComponent != null)
                this.particleEmitterComponent.Update(deltaTime);


            if(this.children != null)
            for(Entity e: this.children)
            {
                if(e != null)
                    e.Update(deltaTime);
            }
       
    }


/*
    @Override
    public void Render(Graphics2D g)
    {
        this.GetRenderableComponent().Render(g);
        //g.drawRect((int)this.GetPhysicsComponent().getPosition().getX(),
        //        (int)this.GetPhysicsComponent().getPosition().getX(), 2, 2);
    }
*/

}
[/code]

[code=WeaponsComponent]public class WeaponComponent extends Component{

    int cooldownTime;    // cooldown between bursts (ms)
    int burstTime;       // time between shots (ms)
    int burstCount;      // number of shots to fire per burst
    int timer;           // miliseconds since last state-change.

    boolean firing = false;
    Entity target;

    float acuracyOffset;

    Vector2D offset;

    Projectile projectileTemplate;

    public WeaponComponent(int i, Entity p)
    {
        super(i,p);

        this.cooldownTime = 500;
        this.burstTime = 0;
        this.burstCount = 1;
        this.offset = new Vector2D();
        this.acuracyOffset = 0.05f;

        initProjectile();
    }



    public void SetTarget(Entity e)
    {
        this.target = e;
    }
    public void Fire()
    {
        if(this.target != null)
            firing = true;
    }
    public void CeaseFire()
    {
        firing = false;
    }

    @Override
    public void Update(int deltaTime)
    {
        if(firing)
        {
            //System.out.println("Weapon Timer: " + this.timer);
            if(this.timer < this.cooldownTime)
            {
                timer += deltaTime;
            }
            else if(this.timer >= this.cooldownTime)
            {
                Vector2D V2T = Vector2D.Sub(this.Parent().GetPhysicsComponent().getPosition(),
                    this.target.GetPhysicsComponent().getPosition());

                double D2T = V2T.getLengh();
                double A2T = V2T.getAngle();

                Random rand = new Random();
                double defl = (rand.nextDouble() - rand.nextDouble()) * this.acuracyOffset;

                Projectile x = new Projectile(this.Parent(), Vector2D.fromAngle(
                        this.Parent().GetPhysicsComponent().getPosition(),
                        D2T, A2T+defl));

                Main.getGame().GetWorldProjectiles().AddProjectile(x);

                timer = 0;
            }
        }
    }


    private void initProjectile()
    {
        this.projectileTemplate = new Projectile(this.Parent(), new Vector2D());
        //this.projectileTemplate.GetRenderableComponent().GetSprite().load(
        //        "artAssets\\projectiles\\cannonShell1.png");
    }

}
[/code]

I'm afraid that its all rather hacked together and got really messed up yesterday as I was trying random crap to try fix it. I think I need to have a good clean-up of the code I have before going any further.
Offline ddyer

Senior Member


Medals: 5



« Reply #1 - Posted 2010-05-23 19:34:13 »


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.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #2 - Posted 2010-05-23 19:41:02 »

Paste this line of code:

ParticleEmitterComponent.java:32

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #3 - Posted 2010-05-23 21:58:36 »

The only line in the constructor that can cause a null pointer is
1  
Main.getGame().GetParticleEngine().AddEffect(myEffect);

So either
1  
Main.getGame()

or
1  
.GetParticleEngine()

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

Junior Member





« Reply #4 - Posted 2010-05-23 22:39:22 »

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  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.
Offline Karmington

Senior Member


Medals: 1
Projects: 1


Co-op Freak


« Reply #5 - Posted 2010-05-23 23:54:15 »

ParticleEmitterComponent.java:32

means the exception is thrown on line 32 of that file

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #6 - Posted 2010-05-24 06:23:46 »

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:
1  
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:
1  
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:
1  
2  
3  
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:
1  
2  
3  
System.out.println(object);
System.out.println(object.getProperty());
System.out.println(object.getProperty().getComponent());

Which might give you output of
1  
2  
3  
<Object:GHDFGH>
<Property:HVJHVA>
null

In which case you would know the component was null, or you could get
1  
2  
3  
<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.

See my work:
OTC Software
Offline Dreamcatchermatt

Junior Member





« Reply #7 - Posted 2010-05-24 10:03:12 »

Cool, thanks for the masterclass Smiley

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
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #8 - Posted 2010-05-24 13:35:16 »

Easier to just use a breakpoint.

Offline Dreamcatchermatt

Junior Member





« Reply #9 - Posted 2010-05-24 15:43:36 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #10 - Posted 2010-05-24 16:30:18 »

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.

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (17 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!