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

2022/2/24

Help! i get error from a line of code thats not even ran :c

Aaron-aid Aaron-aid

2022/2/24

#
the error states java.lang.NullPointerException at enemySpawner.act(enemySpawner.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) yet line 29 isnt supposted to run till later, whats up ?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class enemySpawner here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class enemySpawner extends Actor
{
       boolean enemys= false;
        int def_Val = 0;
        int counter = def_Val;
        boolean runonce = false;
        int x = 0;
      boolean level2check = false;
    /**
     * Act - do whatever the enemySpawner wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if(getWorld().getObjects(Player.class).get(0).currentlv == 2){
        level2check = true;
        mainCode();
        
    }
    
    if(getWorld().getObjects(Player.class).get(0).currentlv == 5 &&level2check == true){
        level_5();
    }
    
        
    }
    void mainCode()
    {
        if(enemys == true){
            getWorld().addObject(new enemy(), getX(), getY());
            getWorld().showText(""+getWorld().getObjects(enemy.class).get(0).num, getX(), getY());
            if(getWorld().getObjects(enemy.class).get(0).num >= 4)
            {
                getWorld().removeObject(this);               
            }
            
            }
    }
    void level_5(){
            getWorld().addObject(new enemy(), getX(), getY());
            getWorld().showText(""+getWorld().getObjects(enemy.class).get(0).num, getX(), getY());
            if(getWorld().getObjects(enemy.class).get(0).num >= 8)
            {
                getWorld().removeObject(this);               
            }
    
    }
    }

danpost danpost

2022/2/24

#
Problem is that line 29 is not even inside a code block.
RcCookie RcCookie

2022/2/24

#
Please format your indents properly. It is extremely hard to read. getWorld() returns null because the object was removed in mainCode(). You should check getWorld() != null first.
RcCookie RcCookie

2022/2/24

#
danpost wrote...
Problem is that line 29 is not even inside a code block.
That's what I thought, too, but it's the horrible indents.
danpost danpost

2022/2/24

#
danpost wrote...
Problem is that line 29 is not even inside a code block.
Ignore this. I missed the open bracket at the end of a line due to inconsistent bracket placements and indentations. LOL: the error trace even shows the line is in the act method.
danpost danpost

2022/2/24

#
In general, I would think that an enemySpawner object is unnecessary (meaning, the class, itself, is not needed). The act method of your World subclass should control the spawning of enemies. This should be evidenced by how just about every line in your code above starts with getWorld..
Aaron-aid Aaron-aid

2022/2/28

#
I realize it looks confusing but the enemy spawner is used in multiple levels and thus needs to change and adapt to each use (its used on lv 2 & 5) and I feel like it would be better management to make the code more flexible and have more opportunity's to be used but I can try to convert each l that uses it to the world exclusively
danpost danpost

2022/3/1

#
Aaron-aid wrote...
I realize it looks confusing but the enemy spawner is used in multiple levels and thus needs to change and adapt to each use (its used on lv 2 & 5) and I feel like it would be better management to make the code more flexible and have more opportunity's to be used but I can try to convert each l that uses it to the world exclusively
Maybe you need an intermediate class between World and your level worlds. Then you can put the spawning code there and use the instanceof conditional to check which type level world it actually is.
You need to login to post a reply.