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

2017/4/9

Getting an error that i do not know how to fix

Wasupmacuz Wasupmacuz

2017/4/9

#
I just wrote some code in a class called "Player" that creates a bottom sensor to detect things below the Player. However, when I try to run the project, I get this error in the Terminal Window:
java.lang.StackOverflowError
	at java.util.HashMap.putVal(HashMap.java:634)
	at java.util.HashMap.put(HashMap.java:611)
	at greenfoot.core.ImageCache.addCachedImage(ImageCache.java:75)
	at greenfoot.util.GreenfootUtil.addCachedImage(GreenfootUtil.java:788)
	at greenfoot.GreenfootImage.<init>(GreenfootImage.java:116)
	at Player.<init>(Player.java:14)
	at BottomSensor.<init>(BottomSensor.java:9)
	at Player.<init>(Player.java:24)
The first parts of my code for the Player is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The person that the user controls
 * 
 * @author Wasupmacuz
 * @version 0.0.0
 */
public class Player extends Actor
{
    private static int walking;
    private static int walkDirection;
    public static boolean isWalking=false;
    GreenfootImage stand=new GreenfootImage("playerStand.png");
    GreenfootImage walk1=new GreenfootImage("playerWalk1.png");
    GreenfootImage walk2=new GreenfootImage("playerWalk2.png");
    private static String leftOrRight="left";
    
    /**
     * Makes necessary things at the start of everything
     */
    public Player()
    {
        BottomSensor bottom=new BottomSensor();
        getWorld().addObject(bottom,getX(),getY()-26);
    }
}
The code for the BottomSensor is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BottomSensor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BottomSensor extends Player
{
    /**
     * Act - do whatever the BottomSensor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}
Thank you guys!
Wasupmacuz Wasupmacuz

2017/4/9

#
Wasupmacuz wrote...
Well I fixed the problem by creating the BottomSensor in the world but I would still like to know what went wrong if anyone knows.
danpost danpost

2017/4/9

#
You have the BottomSensor class extending the Player class. So, when you create a BottomSensor object, you have the Player class constructor creating another one, and another one and so on. -- until your either you run out of memory or the stack overflows (whichever occurs first, which in your case was the stack overflow). A BottomSensor object is not a player and therefore its class should not extend the Player class -- it should extend the Actor class.
Wasupmacuz Wasupmacuz

2017/4/9

#
danpost wrote...
You have the BottomSensor class extending the Player class. So, when you create a BottomSensor object, you have the Player class constructor creating another one, and another one and so on. -- until your either you run out of memory or the stack overflows (whichever occurs first, which in your case was the stack overflow). A BottomSensor object is not a player and therefore its class should not extend the Player class -- it should extend the Actor class.
Okay thank you Dan. Stupid mistake, I guess :)
You need to login to post a reply.