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

2024/2/3

java.lang.NullPointerException

equopan equopan

2024/2/3

#
I cant find the error in my code. The Player should move in a different speed ( fast and slow ). there is an error in the movement code but i cant find it. here is the code: public void move() { if(Waffe.equals("Pfeil")) { if(Greenfoot.isKeyDown("w")) { this.setLocation(this.getX(), this.getY() - 5); } if(Greenfoot.isKeyDown("a")) { this.setLocation(this.getX() - 5, this.getY()); if(timer2==0) { this.getImage().mirrorHorizontally(); this.mirror = false; this.timer2 = timer2+1; } } if(Greenfoot.isKeyDown("s")) { this.setLocation(this.getX(), this.getY()+5); } if(Greenfoot.isKeyDown("d")) { this.setLocation(this.getX() + 5, this.getY()); if(mirror == false) { this.getImage().mirrorHorizontally(); this.mirror=true; this.timer2 = 0; } } } if(Waffe.equals("Blue")) { if(Greenfoot.isKeyDown("w")) { this.setLocation(this.getX(), this.getY() - 2); } if(Greenfoot.isKeyDown("a")) { this.setLocation(this.getX() - 2, this.getY()); if(timer6==0) { this.getImage().mirrorHorizontally(); this.mirror = false; this.timer6 = timer6+1; } } if(Greenfoot.isKeyDown("s")) { this.setLocation(this.getX(), this.getY()+2); } if(Greenfoot.isKeyDown("d")) { this.setLocation(this.getX() + 2, this.getY()); if(mirror == false) { this.getImage().mirrorHorizontally(); this.mirror=true; this.timer6 = 0; } } } } I need this to work for my projekt. Thank you
equopan equopan

2024/2/3

#
And here is the full error message: java.lang.NullPointerException at Player.move(Player.java:171) at Player.act(Player.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
nccb nccb

2024/2/3

#
It's a bit tricky without knowing the exact line numbers but the only things that I can see that can be null are Waffe and this.getImage(). (I think in more recent Java/Greenfoot, it should tell you what exactly was null, incidentally.) If Waffe can be null, you might prefer to write either "Blue".equals(Waffe) or Objects.equals(Waffe, "Blue"), both of which handle the possibility of Waffe being null without throwing an exception. If it's not Waffe, it's because your image is null.
equopan equopan

2024/2/4

#
Ok so now i dont get an error message, but i still cant move the player. The String Waffe is never null because its assigned by starting the game and changed inside the game. here is the code maybe you can see the problem: In this world is the play button: public class Start extends World { protected GreenfootSound music = new GreenfootSound("01 Title Theme.mp3"); private String Waffe; public Start() { super(1000, 700, 1); GreenfootImage bg = this.getBackground(); bg.scale(1000, 700); this.setBackground(bg); this.Waffe = "Pfeil"; prepare(); } private void prepare() { StartButton startButton = new StartButton(Waffe); addObject(startButton,242,221); startButton.setLocation(this.getWidth()/2,this.getHeight()/2); } This is the start button: public StartButton(String Waffen) { this.getImage().scale(this.getImage().getWidth()*3, this.getImage().getHeight()*3); this.Waffe = Waffen; } public void act() { this.setLocation(this.getWorld().getWidth()/2, this.getWorld().getHeight()/2); pPlayerX = 472; pPlayerY = 646; this.button(inventar, pPlayerX, pPlayerY, Waffe); } public void button(ArrayList inventar, int pPlayerX, int pPlayerY, String Waffe) { if(Greenfoot.mouseClicked(this)) //soll nur funktionieren wenn es den button klickt { this.Level = 0; Greenfoot.setWorld( new Lobby(inventar, pPlayerX, pPlayerY, rotationStart, Level, Waffe)); } } and this is the lobby in wich the player gets assigned the attributes: public Lobby(ArrayList inventar, int pPlayerX, int pPlayerY, int rotation, int Livel, String Waffen) { super(1000, 700, 1); this.getBackground().scale(this.getWidth()*2, this.getHeight()*2); this.pInventar = inventar; this.coordinatesX = pPlayerX; this.coordinatesY = pPlayerY; this.pRotation = rotation; this.Level = Livel; prepare(pInventar, pPlayerX, pPlayerY, pRotation, Waffe); this.Waffe = Waffen; } private void prepare(ArrayList pInventar, int pPlayerX, int pPlayerY, int pRotation, String Waffe) { Player player = new Player(Level, Waffe); addObject(player , coordinatesX , coordinatesY); player.setRotation(pRotation); } this is the code where the player gets assigned the attributes: public Player(int Livel, String Waffen) { this.getImage().scale(this.getImage().getWidth()*2, this.getImage().getHeight()*2); this.Level = Livel; this.Waffe = Waffen; } please look into it again
equopan equopan

2024/2/4

#
I inspected the player and waffe is for some reason null. So there has to be a problem with the code at some point
danpost danpost

2024/2/4

#
equopan wrote...
I inspected the player and waffe is for some reason null. So there has to be a problem with the code at some point
You show this code:
public Lobby(ArrayList inventar, int pPlayerX, int pPlayerY, int rotation, int Livel, String Waffen)
{    
    super(1000, 700, 1); 
    this.getBackground().scale(this.getWidth()*2, this.getHeight()*2);
    this.pInventar = inventar;
    this.coordinatesX = pPlayerX;
    this.coordinatesY = pPlayerY;
    this.pRotation = rotation;
    this.Level = Livel;
    prepare(pInventar, pPlayerX, pPlayerY, pRotation, Waffe);
    this.Waffe = Waffen;
}
The prepare method is being called before the Waffe variable is being assigned a value. No wonder its value is null. Switch the last two lines around.
You need to login to post a reply.