0

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.

Bosak
  • 2,103
  • 4
  • 24
  • 43
  • If you need more code tell me here in the comments – Bosak Jan 03 '12 at 21:30
  • 1
    possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Jan 03 '12 at 21:30
  • 1
    @JohnSaunders I would highly doubt that since almost all nullreferences are different. – Laurence Burke Jan 03 '12 at 21:32
  • 1
    @John Saunders There is a difference between What is a _____ exception and Why am I getting _____ exception. You can understand what an exception is for but not understand or know what part of your code is the cause of it. – Josh Mein Jan 03 '12 at 21:34
  • @LaurenceBurke, and yet the answer is generally the same. – Anthony Pegram Jan 03 '12 at 21:35
  • If you set a breakpoint after the line that sets Planet.Image, is Image null? – sq33G Jan 03 '12 at 21:35
  • 3
    @LaurenceBurke: Quite the reverse: every NullReferenceException has the same cause, and the steps taken to diagnose it are generally the same too. – Jon Skeet Jan 03 '12 at 21:36
  • @JonSkeet so yes they will be similar but not duplicate. Therefore different – Laurence Burke Jan 03 '12 at 21:37
  • @jmein: did you read the linked question and its answers? It's about both the causes and the fixes. – John Saunders Jan 03 '12 at 21:40
  • @sq33G Wow it didn't execute the Image property. – Bosak Jan 03 '12 at 21:41
  • 1
    @LaurenceBurke: sorry, I totally disagree. Every case of, "I didn't set it, then I used it" is the exact same mistake. The linked question shows many different examples of this, to make it easier to recognize them by sight. – John Saunders Jan 03 '12 at 21:41
  • 1
    Does the other side of the Planet class have a constructor that takes only 4 arguments? – sq33G Jan 03 '12 at 21:46
  • @LaurenceBurke: I don't buy that - it's sufficiently similar, that if the OP reads through the linked question, they're likely to be able to work out their own answer. – Jon Skeet Jan 03 '12 at 21:49
  • @sq33G Yes it has `public Planet(string name, Image image, float degreesAddedEachTick, bool clockwise){...}` – Bosak Jan 03 '12 at 21:56
  • ...does a breakpoint put in there get hit? – sq33G Jan 03 '12 at 22:04
  • @sq33G Yes it got hit but I got surprised when I set the `Image` the `image` parameter was not null but when the `this.Image = Image` got ex....Wait a sec..FOUND MY PROB!! I am doing `this.Image = Image` and not `this.Image = image` – Bosak Jan 03 '12 at 22:07
  • @JohnSaunders et al. **Bad Naming Convention**? – sq33G Jan 03 '12 at 22:21
  • @LaurenceBurke: Notice the similarity? – John Saunders Jan 03 '12 at 22:28

3 Answers3

2

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

AllenG
  • 8,112
  • 29
  • 40
1

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.

sq33G
  • 3,320
  • 1
  • 23
  • 38
0

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.

Daniel Moses
  • 5,872
  • 26
  • 39
  • It is a native int. I get the exception for the image not for the with . – Bosak Jan 03 '12 at 21:38
  • Then i suggest putting a `if (image == null) throw new IllegalArgumentException();` in the Planet constructor. See if that gives you a different error. Should be able to tell who is setting a null image. – Daniel Moses Jan 03 '12 at 21:41
  • No it gives the same exception – Bosak Jan 03 '12 at 21:58
  • Then you are doing 1 of two things: not actually using that costructor... i noticed you use a constructor with 4 parameters? Or you change the image member variable to null after it is instantiated. – Daniel Moses Jan 03 '12 at 22:11