Bosak
Bosak

Reputation: 2153

Why do I get NullReferenceException here?

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.

Upvotes: 0

Views: 179

Answers (3)

sq33G
sq33G

Reputation: 3360

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.

Upvotes: 1

AllenG
AllenG

Reputation: 8190

It looks like you're never instantiating your 'Planets' object (probably a List<Planet>?)

Is it in your StarSystem code?

Presumably it would be something like

public class StarSystem
{
    public List<Planet> planets {get; set;}
    //Other code here

    public StarSystem()
    {
        planets = new List<Planet>();
    }
}

But that code is not in evidence in your post. That's what I find to be the most likely culprit.

Post Question Edit: Okay, then the only thing that seems to leave is the .Image for one or more of your planets. When you debug, you should be able to watch your variables- I would set a break-point before it evaluates that expression, and look at the last object it added, and the next object it's going to try to add, and look for a null .Image

Upvotes: 2

Daniel Moses
Daniel Moses

Reputation: 5858

You create each planet with this constructor

public Planet(string name, Image image, float distanceFromSun, float degreesAddedEachTick, bool clockwise, PointF rotationCenter, float angleInDegrees = 0)

Yet you never check to make sure that image != null && image.Width != null (if width is a native int you don't need to, but if it is a Number you do need that check.

Upvotes: 0

Related Questions