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

2023/3/3

How do I fix this null pointer execption?

RomanDavis RomanDavis

2023/3/3

#
j
if (!getWorld().getObjects(Counter.class).isEmpty()){
        Counter counter = (Counter) getWorld().getObjects(Counter.class).get(0);
        int counterValue = counter.getValue();
        if(counterValue == 40)
          {
            getWorld().removeObject(this);
          }
      }
That is my code, I am getting the error on "if (!getWorld().getObjects(Counter.class).isEmpty())" How do I solve this?
RomanDavis RomanDavis

2023/3/3

#
Even if I take that line out, I still get the java.lang.NullPointerException
danpost danpost

2023/3/4

#
RomanDavis wrote...
Even if I take that line out, I still get the java.lang.NullPointerException
Need to see the full method (at least).
RomanDavis RomanDavis

2023/3/6

#
public class Snake extends Actor
{
    /**
     * Act - do whatever the Snake wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static int speed = 1;
    
    public void act() 
    {
      chase();
      checkPlayerCollision();
      
        Counter counter = (Counter) getWorld().getObjects(Counter.class).get(0);
        int counterValue = counter.getValue();
        if(counterValue == 10)
          {
            getWorld().removeObject(this);
          }
         
    } 
    //Snake turns to Players X and Y coordinated and moves toward it
    // at is constant speed of "speed"
    public void chase()
    {
        turnTowards(Player.PlayerX, Player.PlayerY);
        move(speed);
    }
    //Adds +1 to the "speed"
    public static void increaseSnakeSpeed(){
         speed ++;
    }
    //Checks if player space is occupied by the snake, if its true
    //Background changes to "game over" screen
    //Berry, Snake, and the Player will be deleted
    public static boolean isDead;
    public void checkPlayerCollision()
    {
        Player player = (Player)getOneIntersectingObject(Player.class);

        
        if(player != null)
        {
            GreenfootImage background = new GreenfootImage("Snake game - game over.png");
            getWorld().setBackground(background);
            speed = 0;
            player.playerSpeed = 0;
            getWorld().removeObject(player);
            Greenfoot.playSound("gameOver.mp3");
            
            
            if (!getWorld().getObjects(Berry.class).isEmpty()){
            Actor berry = getWorld().getObjects(Berry.class).get(0);
            System.out.println(berry.getX());
            getWorld().removeObject(berry);
        }
        getWorld().removeObject(this);
        }
    }
    
    
}
Here is the entire code, of Snake
RomanDavis RomanDavis

2023/3/6

#
Also don't worry about the public static Boolean isDead I will remove that.
danpost danpost

2023/3/7

#
RomanDavis wrote...
<< Code Omitted >> Here is the entire code, of Snake
Your code has obviously changed since your first code post as the line originally given is not shown in the class code just provided. Your original question no longer is applicable. What is the issue, now (with the code of the class as provided)? If you are getting an error, please provide a copy of the Error Trace from the error output. Your use of static content is a concern (as a side note). Player.PlayerX, Player.PlayerY and Snake.speed seem to be variables related to instances of the classes (not anything belonging to the class itself) and should not be static. When removing static from their declaration lines, the same must be done with any methods dealing with them (i.e. increaseSnakeSpeed) Q: Is there any time when more than one snake is in the world? If so, I can see issues with your code.
RomanDavis RomanDavis

2023/3/7

#
I have fixed the issue regarding the null pointer exception after a bit of time but, what do you recon I do to remove the static and leave no error because when delete "static" I get, "non-static variable 'variable name' cannot be referenced from a static context" 'variable name' for example can be "PlayerX" And for your question there will be and only be one snake, and one player.
danpost danpost

2023/3/7

#
RomanDavis wrote...
what do you recon I do to remove the static and leave no error because when delete "static" I get, "non-static variable 'variable name' cannot be referenced from a static context" 'variable name' for example can be "PlayerX"
To get the location coordinates of the player, use something like:
if ( ! getWorld().getObjects(Player.class).isEmpty())
{
    Player player = (Player)getWorld().getObjects(Player.class).get(0);
    turnTowards(player.getX(), player.getY());
    move(speed);
}
You could keep a reference to the Player instance in the Snake instance to make things a little easier, as follows:
private Player player;

protected void addedToWorld(World world)
{
    player = (Player)world.getObjects(Player.class).get(0);
}

public void act()
{
    if (player.getWorld() != null)
    {
        turnTowards(player.getX(), player.getY());
        move(speed);
    }
    // etc.
Just make sure the player is added into the world before the snake.
RomanDavis RomanDavis

2023/3/7

#
Thank you.
You need to login to post a reply.