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

2013/1/9

Constructor Errors

1
2
Gingervitis Gingervitis

2013/1/9

#
I am making a game with various animals and when i try to add a new class (which the player controls), it says that the constructor has not been defined yet every other animal works. Why is that?
   /**
   
  *  Create a new Turtle and place it randomly
     */
    public void createTurtle()
    {
        Turtle newTurtle;
        newTurtle = new Turtle();  // The error is in this line where it says new Turtle();

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newTurtle, x, y);
    }
danpost danpost

2013/1/9

#
You will have to show your Turtle class for help on this (the whole class).
Gingervitis Gingervitis

2013/1/9

#
Do you mean to show my entire class for turtle? Because i have a lot of stuff....
import greenfoot.*; 

public class Turtle extends Animal
{
    private Counter counter;
    public Turtle(Counter pointCounter)
    {
        counter = pointCounter;
    }
    int x =Greenfoot.getRandomNumber(4) + 4;
    // if you want to have the turtle move at a random speed,
    //switch "4" in move to the int variable "x"
    public void act()
    {
        move(4);
        
        tryToEat();
        tryToEatStarfish();
        tryToEatCrab();
        ifAtEdge();
        tryToEatUD();
        tryToEatSUD();
        
    
    
        if (Greenfoot.mouseClicked(this))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();           
            
         createTurtle(); 
        }
    
        
    }

    /**
     * If turtle reaches edge of world, turn (x) amount
     */
    public void ifAtEdge()
    {
        if ( atWorldEdge() )
        {
            if(canSee(Destroyer.class))
            {
                eat(Destroyer.class);
                counter.add(150);
                Greenfoot.playSound("slurp.wav");
                createNewDestroyer2();
                createNewUltimateDestroyer();
                createNewCrab();
                createNewCrab();
                createNewStarfish();
            }
            if (canSee(SuperUltimateDestroyer.class))
            {
                eat(SuperUltimateDestroyer.class);
                Greenfoot.playSound("slurp.wav");
                counter.add(750);
                gameOver();
                
            }

        } 
    }

    public void tryToEatSUD()    
    {
        if (canSee(SuperUltimateDestroyer.class))
        {
            eat(SuperUltimateDestroyer.class);
            Greenfoot.playSound("slurp.wav");
            counter.add(750);
            gameOver();
        }
    }

    public void tryToEatUD()
    {
        if(canSee(UltimateDestroyer.class))
        {
            eat(UltimateDestroyer.class);
            counter.add(500);
            Greenfoot.playSound("slurp.wav");
            createNewSuperUltimateDestroyer();
            move(10);
        }
    }

    

    /**
     * If turtle can see lettuce or Destroyer and sometimes a snake, 
     * it will eat them 
     */
    public void tryToEat()
    {
        if(canSee(Lettuce.class))
        {
            eat(Lettuce.class);
            counter.add(5);
            Greenfoot.playSound("slurp.wav");

        }
        if(canSee(Bug.class))
        {
            eat(Bug.class);
            counter.add(20);
            Greenfoot.playSound("slurp.wav");
            createNewBug();
            createNewBug();
            createNewSnake();
            if(canSee(Destroyer.class))
            {
                eat(Destroyer.class);
                counter.add(150);
                createNewDestroyer2();
                createNewUltimateDestroyer();
                createNewCrab();
                createNewCrab();
                createNewStarfish();
            }
        }
        if (canSee(Snake.class))
        {
            eat(Snake.class);
            counter.add(75);
            createNewDestroyer();
            if(canSee(Destroyer.class))
            {
                eat(Destroyer.class);
                counter.add(150);
                createNewDestroyer2();
                createNewUltimateDestroyer();
                createNewCrab();
                createNewCrab();
            }
        }
        if (canSee(Counter.class))
        {
            if(canSee(Destroyer.class))
            {
                eat(Destroyer.class);
                counter.add(150);
                createNewDestroyer2();
                createNewUltimateDestroyer();
                createNewCrab();
                createNewCrab();

            }
            if(canSee(Starfish.class))
            {
                eat(Starfish.class);
                counter.add(5);
                Greenfoot.playSound("slurp.wav");
                createNewStarfish();
                createNewCrab();
                createNewCrab();
            }

            if (canSee(Crab.class))
            {
                eat(Crab.class);
                counter.add(75);
                createNewCrab();
                createNewStarfish();
                createNewStarfish();
            }
            if (counter.getValue() == 1000  ) 
            {
                gameOver();
            }
        }
    }

    public void tryToEatStarfish()
    {
        if(canSee(Starfish.class))
        {
            eat(Starfish.class);
            createNewStarfish();
            createNewCrab();
        }
    }

    public void tryToEatCrab()
    {
        if (canSee(Crab.class))
        {
            eat(Crab.class);
            createNewCrab();
            createNewStarfish();
        }
    }

    /**
     * We won the game.
     */
    public void gameOver()
    {
        Greenfoot.playSound("gamefinish.wav");
        Greenfoot.stop();
    }

    /**
     *  Create a new bug and place it randomly
     */
    private void createNewBug()
    {
        Bug newBug;
        newBug = new Bug();

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newBug, x, y);
    }

    /**
     *  Create a new snake and place it randomly
     */
    private void createNewSnake()
    {
        Snake newSnake;
        newSnake = new Snake();

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newSnake, x, y);
    }

    /**
     *  Create a new animal that eats everything but you and place it randomly
     */
    private void createNewDestroyer()
    {
        Destroyer newDestroyer;
        newDestroyer = new Destroyer();

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newDestroyer, x, y);
    }

    /**
     *  Create a new animal that eats everything but you and place it randomly
     */
    private void createNewDestroyer2()
    {
        Destroyer2 newDestroyer2;
        newDestroyer2 = new Destroyer2();

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newDestroyer2, x, y);
    }

    /**
     *  Create a new bug and place it randomly
     */
    private void createNewUltimateDestroyer()
    {
        UltimateDestroyer newUltimateDestroyer;
        newUltimateDestroyer = new UltimateDestroyer();

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newUltimateDestroyer, x, y);
    }

    /**
     *  Create a new starfish and place it randomly
     */
    private void createNewStarfish()
    {
        Starfish newStarfish;
        newStarfish = new Starfish();

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newStarfish, x, y);
    } 

    /**
     *  Create a new crab and place it randomly
     */
    private void createNewCrab()
    {
        Crab newCrab;
        newCrab = new Crab();

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newCrab, x, y);
    }

    /**
     *  Create a new bug and place it randomly
     */
    private void createNewSuperUltimateDestroyer()
    {
        SuperUltimateDestroyer newSuperUltimateDestroyer;
        newSuperUltimateDestroyer = new SuperUltimateDestroyer();

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newSuperUltimateDestroyer, x, y);
    }
        /**
     *  Create a new Turtle and place it randomly
     */
    public void createTurtle()
    {
        Turtle newTurtle;
        newTurtle = new Turtle();

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newTurtle, x, y);
    }
    }
danpost danpost

2013/1/9

#
Yes, but in all that code, you do not have a constructor without any parameters. You only have the one that takes a Counter object. So trying to create one with 'new Turtle()' without a Counter object supplied within the parenthesis will cause this error.
Gingervitis Gingervitis

2013/1/9

#
I tried that before and it didn't work. Let me try it again.
Gingervitis Gingervitis

2013/1/9

#
I just did what you told be to do, but now I have a new problem.... I have no control of the turtle now. Any suggestions?
Gingervitis Gingervitis

2013/1/9

#
I think I know what do for that issue.
Gingervitis Gingervitis

2013/1/9

#
Thanks for the error help. I appreciate it. I just started Greenfoot over the weekend.
danpost danpost

2013/1/9

#
I really did not say what to do about it, the choice was up to you. Either (1) create a new constructor in the Turtle class that does not contain a Counter parameter or (2) add a Counter to the statement that calls the constructor. As I do not know which way you went, I cannot give additional advice at this time. However, should you include what you did and what control you are wanting on BOTH original and new Turtle objects, and also (for some insight as to what may be going on) a reason why you start with one Turtle object with a Counter, and when you click on that object you create another Turtle object (with or without one); and what of the original one, what happens with it?
Gingervitis Gingervitis

2013/1/9

#
it is hard to tell what happens to the other turtle because they move with my control for about 3 seconds and then the game stops due to a java error, but that is only when two or more turtles are on screen.
danpost danpost

2013/1/9

#
Do you know how to read the error message? Clear the terminal screen, run the scenario to the error, and copy/paste the message here for assistance.
Gingervitis Gingervitis

2013/1/9

#
I'm not really a computer programer. I just watched a couple of videos to learn about it more. java.lang.NullPointerException at Turtle.tryToEat(Turtle.java:120) at Turtle.act(Turtle.java:22) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
 /**
     * If turtle can see lettuce or Destroyer and sometimes a snake, 
     * it will eat them 
     */
    public void tryToEat()
    {
        if(canSee(Lettuce.class))
        {
            eat(Lettuce.class);
            counter.add(5);
            Greenfoot.playSound("slurp.wav");

        }
        if(canSee(Bug.class))
        {
            eat(Bug.class);
            counter.add(20);
            Greenfoot.playSound("slurp.wav");
            createNewBug();
            createNewBug();
            createNewSnake();
        }
        if(canSee(Destroyer.class))
        {
            eat(Destroyer.class);
            counter.add(150);
            createNewDestroyer2();
            createNewUltimateDestroyer();
            createNewCrab();
            createNewCrab();
            createNewStarfish();
        }

        if (canSee(Snake.class))
        {
            eat(Snake.class);
            counter.add(75);
            createNewDestroyer();
        }
        if(canSee(Destroyer.class))
        {
            eat(Destroyer.class);
            counter.add(150);
            createNewDestroyer2();
            createNewUltimateDestroyer();
            createNewCrab();
            createNewCrab();
        }
    
    if (canSee(Counter.class))
    {
        if(canSee(Destroyer.class))
        {
            eat(Destroyer.class);
            counter.add(150);
            createNewDestroyer2();
            createNewUltimateDestroyer();
            createNewCrab();
            createNewCrab();

        }
        if(canSee(Starfish.class))
        {
            eat(Starfish.class);
            counter.add(5);
            Greenfoot.playSound("slurp.wav");
            createNewStarfish();
            createNewCrab();
            createNewCrab();
        }

        if (canSee(Crab.class))
        {
            eat(Crab.class);
            counter.add(75);
            createNewCrab();
            createNewStarfish();
            createNewStarfish();
        }
        if (counter.getValue() == 1000  ) 
        {
            gameOver();
        }
    }
}
public void act()
    {
        move(4);

        tryToEat();
        tryToEatStarfish();
        tryToEatCrab();
        ifAtEdge();
        tryToEatUD();
        tryToEatSUD();
        checkKeys();
        mouseClick();
    }
danpost danpost

2013/1/9

#
at Turtle.tryToEat(Turtle.java:120) That is the line that tells you where the error took place -Turtle class - tryToEat method - line 120 in the class Which line is line 120 in the Turtle class code (you can display line numbers by Edit>Preferences /Editor tab/'Display line number' checkbox). I fear it is the line 'counter.add(5);' because you do not have a counter assigned to the Counter field for the new Turtle object. The error occurs the moment the new Turtle object finds a piece of lettuce.
Gingervitis Gingervitis

2013/1/9

#
It says there is an error, but i don't see anything that needs to be changed.
danpost danpost

2013/1/9

#
Let me explain something. The initial Turtle object you create was given a Counter object to work with (I would like to know if it was added to the world or not; though this does interfere with the workings thereof). The new Turtle object was NOT given a Counter object to work with, so its 'counter' field is still 'null' (does not contain a Counter object). When the new Turtle object finds lettuce, your code tries to add 5 points to the non-existent counter and causes an error (because 'counter' is 'null'; hence NullPointerException). The easiest fix is to change the line
counter.add(5);
// TO
if(counter != null) counter.add(5);
But, then the second Turtle object will still not have a Counter object to work with. If you want a new Counter object to be assigned to the new Turtle object, just create a new one before creating the new Turtle object and pass it in the parameter of the constructor call for the new Turtle.
There are more replies on the next page.
1
2