My code of my world is The code of my TankTurret is And my heading code is My error is in the Heading object in line 71
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MainWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MainWorld extends World
{
/**
* Constructor for objects of class MainWorld.
*
*/
public MainWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
Heading heading = new Heading();
TankTurret turret = new TankTurret(heading);
TankBase tank = new TankBase();
addObject(tank, 200, 200);
addObject(turret, 198, 199);
turret.createHeading();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class TankTurret here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class TankTurret extends Actor
{
private Heading heading;
public TankTurret()
{
}
public TankTurret(Heading head)
{
heading = head;
}
/**
* Act - do whatever the TankTurret wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
heading.turnTurret(getRotation());
}
public void turnNow(int degr)
{
turn(degr);
}
public void createHeading()
{
TankTurret turret = new TankTurret(heading);
Heading heading = new Heading(turret);
getWorld().addObject(heading, getX(), getY());
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Heading here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Heading extends Actor
{
private TankTurret turret;
int rotatie;
int rotatieSpeedRechts = 2;
int rotatieSpeedLinks = -2;
public Heading()
{
}
public Heading(TankTurret turr)
{
turret = turr;
if (Greenfoot.getMouseInfo()== null)
{
}
else
{
MouseInfo mouse = Greenfoot.getMouseInfo();
turnTowards((int)mouse.getX(), (int)mouse.getY());
}
}
/**
* Act - do whatever the Heading wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
turn();
}
public void turn()
{
if (Greenfoot.getMouseInfo()== null)
{
}
else
{
MouseInfo mouse = Greenfoot.getMouseInfo();
turnTowards((int)mouse.getX(), (int)mouse.getY());
}
}
public void setRotationSpeed(int rotaSpeedLinks, int rotaSpeedRechts)
{
int rotatieSpeedRechts = rotaSpeedRechts;
int rotatieSpeedLinks = rotaSpeedLinks;
}
public void turnTurret(int rotatie)
{
//turret.setHeading(getRotation());
if(getRotation() > rotatie && getRotation() < rotatie + 180)
{
turret.turnNow(rotatieSpeedRechts);
}
else if(getRotation() > rotatie && getRotation() + 180 < rotatie || getRotation() - 180 < rotatie)
{
turret.turnNow(rotatieSpeedRechts);
}
else if(getRotation() < rotatie && getRotation() + 180 > rotatie)
{
turret.turnNow(rotatieSpeedLinks);
}
else if(getRotation() < rotatie && getRotation() + 180 > rotatie || getRotation() - 180 > rotatie)
{
turret.turnNow(rotatieSpeedLinks);
}
}
}

