I have:
partial class StarSystem : AstroThreeNode
{
public static StarSystem SunSolarSystem()
{
return new StarSystem("Solar System", Planet.SunCenter(), Resources.Space, SolarSystem.ActiveForm.Bounds).AddPlanet(Planet.MercuryPlanet(), Planet.VenusPlanet(), Planet.EarthPlanet(), Planet.MarsPlanet(), Planet.JupiterPlanet(), Planet.SaturnPlanet(), Planet.UranusPlanet(), Planet.NeptunePlanet());
}
public StarSystem AddPlanet(params Planet[] planetsToAdd)
{
foreach (Planet planet in planetsToAdd)
{
distanceFromSun += (planets.Count > 0 ? planets[planets.Count - 1].Image.Width / 2 : 0) + planet.Image.Width / 2 + DISTANCE_BETWEEN_PLANETS;
planet.DistanceFromSun = distanceFromSun;
planet.RotationCenter = new PointF(bounds.Width / 2, bounds.Height / 2);
planets.Add(planet);
}
return this;
}
}
partial class Planet : AstroThreeNode
{
private const float SUN_SPEED = 0;
public static Planet SunCenter()
{
return new Planet("Слънце", Resources.Sun_, SUN_SPEED, CLOCKWISE);
}
public static Planet MercuryPlanet()
{
return new Planet("Меркурий", Resources.Mercury_, 4.0923f, Planet.CLOCKWISE);
}
public static Planet VenusPlanet()
{
return new Planet("Венера", Resources.Venus_, 1.6021f, Planet.COUNTERCLOCKWISE);
}
public static Planet EarthPlanet()
{
return new Planet("Земя", Resources.Earth_, 0.9856f, Planet.CLOCKWISE);
}
public static Planet MarsPlanet()
{
return new Planet("Марс", Resources.Mars_, 0.5240f, Planet.CLOCKWISE);
}
public static Planet JupiterPlanet()
{
return new Planet("Юпитер", Resources.Jupiter_, 0.0830f, Planet.CLOCKWISE);
}
public static Planet SaturnPlanet()
{
return new Planet("Сатурн", Resources.Saturn_, 0.0334f, Planet.CLOCKWISE);
}
public static Planet UranusPlanet()
{
return new Planet("Уран", Resources.Uranus_, 0.0117f, Planet.COUNTERCLOCKWISE);
}
public static Planet NeptunePlanet()
{
return new Planet("Нептун", Resources.Neptune_, 0.0059f, Planet.CLOCKWISE);
}
public Planet(string name, Image image, float distanceFromSun, float degreesAddedEachTick, bool clockwise, PointF rotationCenter, float angleInDegrees = 0)
{
this.Name = name;
this.Image = image; // here I set the Image but I get an exception later
this.DistanceFromSun = distanceFromSun;
this.degreesAddedEachTick = degreesAddedEachTick;
this.isClockwiseRotation = clockwise;
this.RotationCenter = rotationCenter;
this.angleInDegrees = angleInDegrees;
}
}
I get the exception here: distanceFromSun += (planets.Count > 0 ? planets[planets.Count - 1].Image.Width / 2 : 0) + planet.Image.Width / 2 + DISTANCE_BETWEEN_PLANETS;
For the planet.Image. I don’t get it why do I get exception when I set the Image when I create a Planet.
EDIT: Here is the constructor for StarSystem :
public StarSystem(string name, Planet starSystemCenter, Image background, Rectangle bounds)
{
this.Name = name;
this.Background = background;
this.bounds = bounds;
planets = new List<Planet>(); //planets instanieted here
planets.Add(starSystemCenter); // note I dont get the exception on this call but on the other call where I add all other planest
}
EDIT 2:
Found my problem but I have to go to bed I will post the answer tomorrow.
Try to have class members, local variables, and method parameters differ by more than capitalization. See Bad Naming Convention in the canonical NullReferenceException post linked above.