Hello, I am trying to make a kill system where, if a bullet hits the Stalker or Sniper, it kills them when their health is low enough. With the Sniper it works, but with the stalker I get the error :
java.lang.NullPointerException
at Player_Bullet.Stalkerkill(Player_Bullet.java:30)
at Player_Bullet.werk(Player_Bullet.java:53)
at Player_Bullet.act(Player_Bullet.java:17)
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)
The code for both is the same, but with different names and it references the right Actor.
The codes for the involved actors will be standing below (The actors it extends to have nothing to do with the code itself)
This is the code of the bullet that kills the enemies (not the class Enemies)
With this actor it works
With this actor it doesn't work
This is the code of myWorld, it might have something to do with enemies in Player_Bullet, but when I put in stalker it still doesn't work. In fact I get a new error.
the error
java.lang.NullPointerException
at Player_Bullet.Sniperkill(Player_Bullet.java:22)
at Player_Bullet.Checkdelete(Player_Bullet.java:42)
at Player_Bullet.act(Player_Bullet.java:18)
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)
movementTest is just a movement code, it has nothing to do with the errors
I use the same code as the sniper for my player.class, and it just works fine (with name changes ofc.)
import greenfoot.*; public class Player_Bullet extends Bullet { public Sniper sniper; public Stalker stalker; public boolean deleted = false; public int Deletecount = 0; public Player_Bullet (Sniper sniperDirk) { sniper = sniperDirk; } public Player_Bullet (Stalker stalkerDirk) { stalker = stalkerDirk; } public void act() { werk(); Checkdelete(); } public void Sniperkill() { if (isTouching(Sniper.class) && sniper.Sniperhealth == 1 && deleted == false) { getWorld().removeObject(sniper); getWorld().removeObject(this); deleted = true; } } public void Stalkerkill() { if (isTouching(Stalker.class) && stalker.Stalkerhealth == 1 && deleted == false) { getWorld().removeObject(sniper); getWorld().removeObject(this); deleted = true; } } public void Checkdelete() { if (deleted == true) { Deletecount++; } else { Sniperkill(); } if (Deletecount == 1) { deleted = false; } } public void werk() { if (deleted == true) { Deletecount++; } else { Stalkerkill(); } if (Deletecount == 1) { deleted = false; } }
import greenfoot.*; /** * */ public class Sniper extends Enemies { public int Sniperhealth = 3; public Sniper() { UpdatehealthSniper(0); } public void act() { Sniperhit(); movementTest(); } public void Sniperhit() { if (isTouching(Player_Bullet.class) && Sniperhealth == 3) { Sniperhealth = 2; removeTouching(Player_Bullet.class); } if (isTouching(Player_Bullet.class) && Sniperhealth == 2) { Sniperhealth = 1; removeTouching(Player_Bullet.class); } } public void UpdatehealthSniper(int CheckhealthSniper) { Sniperhealth += CheckhealthSniper; } }
import greenfoot.*; /** * */ public class Stalker extends Enemies { public int Stalkerhealth = 3; public Stalker() { UpdatehealthStalker(0); } public void act() { movementTest(); Stalkerhit(); } public void Stalkerhit() { if (isTouching(Player_Bullet.class) && Stalkerhealth == 3) { Stalkerhealth = 2; removeTouching(Player_Bullet.class); } if (isTouching(Player_Bullet.class) && Stalkerhealth == 2) { Stalkerhealth = 1; removeTouching(Player_Bullet.class); } } public void UpdatehealthStalker(int CheckhealthStalker) { Stalkerhealth += CheckhealthStalker; } }
/** * */ public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ public MyWorld() { super(900, 900, 1); prepare(); } private void prepare() { Stalker stalker = new Stalker(); addObject(stalker,441,121); Sniper sniper = new Sniper(); addObject(sniper,244,124); Player player = new Player(); addObject(player,234,484); Enemy_Bullet enemy_Bullet1 = new Enemy_Bullet(player); addObject(enemy_Bullet1,800,100); Enemy_Bullet enemy_Bullet2 = new Enemy_Bullet(player); addObject(enemy_Bullet2,800,200); Enemy_Bullet enemy_Bullet3 = new Enemy_Bullet(player); addObject(enemy_Bullet3,800,300); Enemy_Bullet enemy_Bullet4 = new Enemy_Bullet(player); addObject(enemy_Bullet4,800,400); Player_Bullet player_Bullet = new Player_Bullet(sniper);//or stalker addObject(player_Bullet,100,100); Player_Bullet player_Bullet1 = new Player_Bullet(sniper); addObject(player_Bullet1,100,200); Player_Bullet player_Bullet2 = new Player_Bullet(sniper); addObject(player_Bullet2,100,300); } }