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

2023/2/15

I am getting an error

Shaormentiul Shaormentiul

2023/2/15

#
import lang.stride.*;
import java.util.*;
import greenfoot.*;

/**
 * 
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     */
    public MyWorld()
    {
        super(1168, 750, 1);
        AttackD mainAttackD =  new  AttackD();
        Player mainPlayer =  new  Player();
        Player2 mainPlayer2 =  new  Player2();
        addObject(mainPlayer, 200, 500);
        addObject(mainPlayer2, 1000, 500);
        
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
    }
}
And this is the Player
import lang.stride.*;
import java.util.*;
import greenfoot.*;

/**
 * 
 */
public class Player extends Actor
{
    private int health = 100;
    public int damage = 5;
    private int power = 0;
    private int attackDelay = 0;
    public int damage2 = getWorld().getObjects(Player2.class).get(0).damage2;
    
    
    

    /**
     * 
     */
    public Player()
    {
        GreenfootImage image = getImage();
        image.scale(500, 500);
        setImage(image);
        
        
        
    }

    /**
     * Act - do whatever the Person wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
     */
    final public void act()
    {
        if (attackDelay > 0) {
            attackDelay = attackDelay - 1;
            return;
        }
        int dx = 0;
        if (Greenfoot.isKeyDown("right")) {
            dx = dx + 1;
        }
        if (Greenfoot.isKeyDown("left")) {
            dx = dx - 1;
            
        }
        setLocation(getX() + 5 * dx, getY());
        if ("x".equals(Greenfoot.getKey())) {
            if ( ! Greenfoot.isKeyDown("down")) {
                atac();
            }
            if (Greenfoot.isKeyDown("down")) {
                atacDown();
            }
        }
    }

    /**
     * 
     */
    private void atac()
    {
        attackDelay = 20;
        getWorld().addObject( new  Attack(), getX() + 50, getY());
    }

    /**
     * 
     */
    private void atacDown()
    {
        attackDelay = 20;
        getWorld().addObject( new  AttackD(), getX() + 50, getY() + 50);
    }

    /**
     * 
     */
    public void getDamaged()
    {
        if (isTouching(Attack.class)) {
            health = health - damage2;
        }
    }
}
And this is the error that I am getting in the terminal java.lang.NullPointerException at Player.<init>(Player.java:15) at MyWorld.<init>(MyWorld.java:19)
Shaormentiul Shaormentiul

2023/2/15

#
What is the problem here?
danpost danpost

2023/2/15

#
Shaormentiul wrote...
<< Code Omitted >> And this is the Player << Code Omitted >> And this is the error that I am getting in the terminal << Error Trace Omitted >>
The getWorld method will always have a null value during Player construction. That is, it is useless using it before the actor is added into the world; and, that does not happen until after construction. So, it can only be used within the act method or, which is probably best in this case, in the addedToWorld(World) method (or a method called from one of these). This is referring to line 14 in your Player class. Also, you create this Player object first in your world constructor. This means there is no Player2 object yet created to get the value of damage2 from. The Player2 object also would need to be added into the world before the Player object.
Shaormentiul Shaormentiul

2023/2/15

#
So what would be the right code?
danpost danpost

2023/2/15

#
Shaormentiul wrote...
So what would be the right code?
Add player2 into world before player and use the addedToWorld method to assign the value to damage2. I would rather just get the value when damage occurs, however. That way, you get the actual current value. Use something like:
if (isTouching(Attack.class))
{
    Player2 p2 = (Player2)getWorld().getObjects(Player2.class).get(0);
    health = health - p2.damage2;
}
With this, your Player2 object must always be in the world. Otherwise, you will need to check the list acquired from getObjects to ensure that it is not empty (because you cannot get an element from an empty list). However, I do not foresee that occurring as the Attack object is probably created by the Player2 object. Note: I hope the Attack objects created by the players are not placed into the world in such a way as they touch the player creating them.
Shaormentiul Shaormentiul

2023/2/16

#
Thanks for the help, it doesnt give me the error anymore. The fact that the attack will hit the player creating it wont matter, because it will only affect P2.
You need to login to post a reply.