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

2021/2/10

Get number of all objects from one class

Afroist Afroist

2021/2/10

#
Hello, I am currently working on a space invaders project and no matter what I try I get the same error: java.lang.NullPointerException in terminal when one alien gets removed. I try to get a numAliens so that the game will be over if getObjects(Alien.class).size() == 0. this is how they are spawned in the world
//"spawn" rows and columns of Aliens  
        for(int x=110;x<=390;x+=40)
        {
            for(int y=80;y<=200;y+=40)
            {
                addObject(new Alien(),x,y);
            }
        }
this is how I tried to get the amount of alien objects left in the world class
public int getNumAliens()
    {
        /*List <Alien> aliens = getObjects(Alien.class);
        numAliens = aliens.size();
        return numAliens;
        */
        numAliens = numberOfObjects();
        return numAliens;
    }
and this is how I try to use it in the Alien class
/**
     * Check if all Aliens are removed/ "dead"
     */
    public void checkForWin()
    {
        Space space=(Space)getWorld();
        if(space.getNumAliens() == 0)
        { 
            space.showWin();
        } 
    }
I also tried other if conditions like <2 etc. I think the problem is the object remover it should only remove one specific object and not the whole class
/**
     * Check if alien and shot interesect and remove both when true  
     */
    public void checkForHit(Class intersectingClass)
    {
        Actor actor = getOneIntersectingObject(intersectingClass);
        if(actor!=null)
        {
            Space space = (Space)this.getWorld();
            space.getCounter().add(getPoints());
            space.removeObject(getOneIntersectingObject(Shot.class));
            space.removeObject(this);
            explosionSound.setVolume(70);
            explosionSound.play();
        }
    }
Afroist Afroist

2021/2/10

#
* is it possible to get numberOfObjects(Alien.class); to an int instead of a list?
danpost danpost

2021/2/10

#
Afroist wrote...
* is it possible to get numberOfObjects(Alien.class); to an int instead of a list?
int numAliens = getObjects(Alien.class).size();
(it is basically what you have commented out in getNumAliens method above)
danpost danpost

2021/2/10

#
Afroist wrote...
this is how I try to use it in the Alien class
/**
 * Check if all Aliens are removed/ "dead"
 */
public void checkForWin()
{
    Space space=(Space)getWorld();
    if(space.getNumAliens() == 0)
    { 
        space.showWin();
    } 
}
This code is misplaced. It should be in your Space class (if no aliens in world, then alien trying this check is also not in world; if not in world, getWorld will return a null value).
Afroist Afroist

2021/2/10

#
@danpost thank you very much for helping me out. It is working now as expected
/**
     * Check if all Aliens are removed/ "dead"
     */
    public void checkForWin()
    {
        if(getNumAliens() == 0)
        { 
            showWin();
        } 
    }
    
    /**
     * Get alive aliens
     * @return all objects of the class Alien
     */
    public int getNumAliens()
    {
        List <Alien> aliens = getObjects(Alien.class);
        numAliens = aliens.size();
        return numAliens;
    }
I learned that I have to use getWorld() as shown below to get access to methods of World subclasses Just to get this right my mistake was that getWorld() will only return objects in the World class and the subclass Space (where my aliens are created) will be excluded?
((Space)getWorld()).[I]method[/i]();
                       ||
Space space=(Space)getWorld();
space.[I]method[/i]
You need to login to post a reply.