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

2013/2/27

Help me with this error?

henrYoda henrYoda

2013/2/27

#
Hi guys i've been trying to make a game and I am quite new to java. I have the code here and it works but the problem is when I click to shoot the bullet it shoots and will destroy the other object, but if it goes out of the edge I get an error. Can anyone help me? Here's the code for the bullet class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bullet extends Actor
{
 public void act() 
    {
        move(7);
        disappear();
        collisionDetection();
        //colision detection and point awarding
        
        
        
        //create bullet object
    }
   public void collisionDetection()
   {
    Actor Key = getOneObjectAtOffset(0, 0, Key.class);
            if (Key != null) 
               {
                hitObject();
                getWorld().removeObject(Key);
                getWorld().removeObject(this);
            
    }
}    //make bullet disappear
 public void disappear()
 {
if( getX() <= 4 || getX() >= getWorld().getWidth()-2) 
    
        {
            getWorld().removeObject(this);
            return;
        }
      if( getY() <= 4 || getY() >= getWorld().getHeight()-2) 

        {
            getWorld().removeObject(this);
            return;
        }
    
}      
       
    
    public void hitObject()
    {
        Piano pianoWorld = (Piano) getWorld();
        Counter counter = pianoWorld.getCounter();
        counter.scoreCount(5);
         
            
            
                
            
           
        }
    }


Jonas234 Jonas234

2013/2/27

#
I guess its because you try to call Actor Key = getOneObjectAtOffset(0, 0, Key.class); after the actor is deleted. (I am not really sure though). You could try to add an boolean isDeleted and then confirm that the value is still false before the method is called. Jonas
davmac davmac

2013/2/27

#
There's a fairly complete explanation of this problem here.
danpost danpost

2013/2/27

#
Once the object is removed from the world, the collision checking will error because it has no location in any world to check collision at ('getWorld' return 'null' after 'getWorld().removeObject(this)' is executed; so you will either get a 'NullPointerException' or an 'IllegalStateException' error, depending on the method that caused the error). The best way to avoid the error is to conditional execute the 'collisionDetection' method depending on whether 'getWorld' returns 'null' or not in the 'act' method: if (getWorld() != null) collisionDetection(); As an alternation (and I prefer this way), add the following statement between the calls to 'disappear' and 'collisionDetection' in the 'act' method (this causes an immediate exit from the method if the actor was removed from the world): if (getWorld() == null) return;
henrYoda henrYoda

2013/2/27

#
Thank you danpost, now it works :D
You need to login to post a reply.