I am new to coding, and I have been trying to change the image for my heart icons when the player lands on a spike. My only problem was accessing a variable from another actor, and I followed this tutorial for it: https://www.greenfoot.org/doc/howto-1 (Even though I don't fully understand how everything in it works). I am now getting a NullPointerException Error at line 18 of the player, which is here:
That line links to the health code here:
What is the problem, and how can i fix it?
Thanks
public void SpikeDeath()
{
if (OnSpike())
{
HitCounter = HitCounter + 1;
setLocation (74, 401);
LoseHealth();
}
else if (HitCounter == 3)
{
Greenfoot.setWorld(new YouLose());
}
}
public void LoseHealth()
{
Level1 Level1World = (Level1) getWorld();
Health health = Level1World.getHealth();
health.TakeDamage(1);
}public void TakeDamage(int amount)
{
TotalHealth += amount;
if (TotalHealth == 1)
{
setImage (image2);
}
else if (TotalHealth == 2)
{
setImage (image3);
}
else if (TotalHealth == 3)
{
setImage (image4);
}
}
