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

2020/6/4

How to fix Nullpointerexception ERROR

EthanWasHere_ EthanWasHere_

2020/6/4

#
I have been getting a NullPointerEXception ERROR, Please help me fix this error I have been trying to fix it for the past 2 hours the problem is on line 35, thanks in advance!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Player extends Actor
{
    public boolean isGrounded;
    boolean isJumping;
    boolean atRightEdge;
    boolean atLeftEdge;
    int yVel;
    int jumpTime;
    int jumpTimer;
    public int xVel;
    public Player()
    {
        yVel = 0;
        jumpTime = 10;
        xVel = 4;
    }
    public void act() 
    {
        move();
        jump();
        checkGround();
        checkEdge();
        gravity();
    }    
    public void gravity()
    {
        WorldMethods world = (WorldMethods) getWorld();
        Feet feet = (Feet) world.getFeet();
        
        int gravityForce = -1;
        int terminalVel = -20;
        
        if(!isGrounded && !feet.feetGrounded)
        {
            //doesn't allow the player to go faster than 20 pixels per act cycle
            if(yVel > terminalVel){yVel += gravityForce;}
            else {yVel = terminalVel;}
        }
        else if(!isGrounded && feet.feetGrounded && !isJumping)
        {
            yVel = 0;
            jumpTimer = 0;
        }
        else
        {
            yVel = 1;
        }
        setLocation(getX() ,getY() - yVel);
    }
    public void jump()
    {
        int jumpForce = 10;
        
        if(Greenfoot.isKeyDown("space") && jumpTimer < jumpTime)
        {
            yVel = jumpForce;
            isJumping = true;
        }
        if(yVel <= 0)
        {
            isJumping = false;
        }
        jumpTimer++;
    }
    public void move()
    {
        if(Greenfoot.isKeyDown("a") && !atLeftEdge)
        {
            setLocation(getX() - xVel, getY());
        }
        if(Greenfoot.isKeyDown("d") && !atRightEdge)
        {
            setLocation(getX() + xVel, getY());
        }
    }
    public void checkGround()
    {              
        Ground touchingGround = (Ground) getOneIntersectingObject (Ground.class);
        
        if(touchingGround != null)
        {
            isGrounded = true;
        }
        else
        {
            isGrounded = false;
        }
    }
    public void checkEdge()
    {
        WorldMethods world = (WorldMethods) getWorld();
        
        if(getX() <= world.playerWidth/2)
        {
            atLeftEdge = true;
        }
        else{
            atLeftEdge = false;
        }
        if(getX() >= world.screenWidth - world.playerWidth/2)
        {
            atRightEdge = true;
        }
        else
        {
            atRightEdge = false;
        }
    }


    public void lava()
    {
        if (isTouching(Lava.class))
            setLocation(29, 597);
    }

    public void bang()
    {
        if (isTouching(Bullet.class))
            setLocation(970, 114);
    }
}
danpost danpost

2020/6/4

#
Show getFeet method in WorldMethods class.
EthanWasHere_ EthanWasHere_

2020/6/6

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class WorldMethods extends World
{
    public Player thePlayer;
    public Feet theFeet;
    public int screenWidth;
    public int screenHeight;
    public int playerWidth;
    public int playerHeight;
    public WorldMethods()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1, false); 
    }
    
    public Player getPlayer()
    {
        return thePlayer;
    }
    
    public Feet getFeet()
    {
        return theFeet;
    }
}
danpost danpost

2020/6/6

#
Where do you have "new Feet()" in your code?
You need to login to post a reply.