This site requires JavaScript, please enable it in your browser!
Greenfoot back
BradH
BradH wrote ...

2013/1/12

Run Time error

1
2
BradH BradH

2013/1/12

#
Here is another classic nullpointer exception, haha. I have a constructor in my Bullet1 class for this guardiancounter and once the bullet hits the actor it removes it from the screen (or kills it however you want to call it). Once it is removed I want the object (Bullet1) to tell the guardiancounter to subtract one but when the bullet and the guardian actor intersect I get a run time error.

Actor Guardian;
        Guardian = getOneObjectAtOffset(0, 0, Guardian.class);
        if ( Guardian != null)
        {
            World world;
            world = getWorld();

            world.addObject(new GuardianHealthBar3(),getX() , 290 );
            world.addObject(new Eldardeathdeathrifle(),getX() , getY() );
            world.removeObject(Guardian);
            getWorld().removeObject(this);
            guardiancounter.subtract(1);



            return;

        }
davmac davmac

2013/1/12

#
You don't say what line you get the error on, which makes this a bit hard to answer! But - I think probably you're having a problem because, after you remove 'this' object from the world, getWorld() will return null. You need to re-arrange your code logic so that you do the 'other stuff' before you remove the bullet from the world. Or, save the world in a variable before removing the bullet and use the saved reference instead of calling getWorld() again.
BradH BradH

2013/1/12

#
The error is at guardiancounter.subtract(1); I already tried to put guardiancounter.subtract(1); before World world; but I still had the same issue. I will try it the other way you suggested, thanks
BradH BradH

2013/1/12

#
Hold on, I mityped something. When I put guardiancounter.subtract(1); before World world; I get the error on line 4 if(guardian != null)
vonmeth vonmeth

2013/1/12

#
It looks like you don't have an object assigned to "guardiancounter". Can you show us the code where assign the counter object to it?
BradH BradH

2013/1/13

#
Is this what you want to see, the code where I assign the counter to the Bullet1 class?
 private GuardianCounter guardiancounter;
    public Bullet1(GuardianCounter pointGuardianCounter)
    {
        guardiancounter = pointGuardianCounter;
    }
vonmeth vonmeth

2013/1/13

#
So you still get an error when it is something like this:
		Actor Guardian;  
        Guardian = getOneObjectAtOffset(0, 0, Guardian.class);  
        if ( Guardian != null)  
        {  
			guardiancounter.subtract(1);
			
            World world;  
            world = getWorld();  
  
            world.addObject(new GuardianHealthBar3(),getX() , 290 );  
            world.addObject(new Eldardeathdeathrifle(),getX() , getY() );  
            world.removeObject(Guardian);  
            world.removeObject(this);  
            
            return;  
        }
If so, post the whole code for the class, and the entire error.
BradH BradH

2013/1/13

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet1 extends Ammo
{  private int gone = (90);

    private GuardianCounter guardiancounter;
    public Bullet1(GuardianCounter pointGuardianCounter)
    {
        guardiancounter = pointGuardianCounter;
    }

    /**
     * Act - do whatever the Bullet1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        kill();
        bye();
        Move();
    }

    public void Move()
    {
        move(9);
    }

    public void kill()
    {//finish this later put other guarduan classes in this

        

        Actor Guardian;
        Guardian = getOneObjectAtOffset(0, 0, Guardian.class);
        if ( Guardian != null)
        {
            guardiancounter.subtract(1);
            World world;
            world = getWorld();

            world.addObject(new GuardianHealthBar3(),getX() , 290 );
            world.addObject(new Eldardeathdeathrifle(),getX() , getY() );
            world.removeObject(Guardian);
            getWorld().removeObject(this);

            return;

        }

        Actor GuardianShooter;
        GuardianShooter = getOneObjectAtOffset(0, 0, GuardianShooter.class);
        if ( GuardianShooter != null)
        {
            guardiancounter.subtract(1);
            World world;
            world = getWorld();

            world.addObject(new GuardianHealthBar3(),getX() , 290 );
            world.addObject(new Eldardeathdeathrifle(),getX() , getY() );
            world.removeObject(GuardianShooter);
            getWorld().removeObject(this);
            
        }
    }  

    public void bye()
    {

        gone -- ;
        if(gone == 0)
        {

            getWorld().removeObject(this);
            return;
        } 
    }

}



vonmeth vonmeth

2013/1/13

#
Try something like this in your act method.
public void act()   
    {  
        kill();  
        if (getWorld() != null) bye();  
        Move();  
    }  
Edit: Might have to something like that for Move() as well? Not sure if it will cause an exception after you remove the object. You could just move Move() to the top of act. Reordering kill and bye wouldn't work as they both remove 'this'. Only reason I can see this giving you an error *shrug*.
BradH BradH

2013/1/13

#
I still have the runtime error. I even got rid of the bye(); method but I still had the error. Thanks for your time tho :)
vonmeth vonmeth

2013/1/13

#
Did you also move Move() up or add "if (getWorld() != null)" before it?
BradH BradH

2013/1/13

#
yeah, it still has the error at guardiancounter.subtract(1);
vonmeth vonmeth

2013/1/13

#
I think you are probably not actually passing a guardiancounter object to your constructor, just passing a null.
GuardianCounter guardiancounter;
guardiancounter = new GuardianCounter();   // what you are missing?
// without the above line creating the object, it will compile, but produce an error when you attempt to use it like you are.
Bullet1 bullet = new Bullet1(guardiancounter);
If that isn't the problem, show your entire code for producing a Bullet1.
BradH BradH

2013/1/13

#
So in my gun class (the class that creates the object Bullet1) I should put that code?
vonmeth vonmeth

2013/1/13

#
Right. You have to pass a GuardianCounter object to your Bullet1, or else it will be trying to call the subtract method from null and give you that error.
There are more replies on the next page.
1
2