为什么我在这里得到NullReferenceException?

时间:2012-01-03 21:29:27

标签: c# exception nullreferenceexception

我有:

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;
    }
}

我在这里得到例外:distanceFromSun += (planets.Count > 0 ? planets[planets.Count - 1].Image.Width / 2 : 0) + planet.Image.Width / 2 + DISTANCE_BETWEEN_PLANETS; 对于planet.Image。我不知道为什么在创建Planet时设置图像时会出现异常。

编辑:以下是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
    }

编辑2: 发现我的问题,但我必须上床,我明天会发布答案。

3 个答案:

答案 0 :(得分:2)

看起来你永远不会实例化你的'行星'对象(可能是List<Planet>?)

是否在您的StarSystem代码中?

据推测它会像

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

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

但是你的帖子中没有证明这些代码。这就是我发现最有可能的罪魁祸首。

发布问题编辑: 好的,那么唯一似乎就是你的一个或多个行星的.Image。在调试时,您应该能够看到变量 - 我会在评估该表达式之前设置一个断点,然后查看它添加的最后一个对象,以及它要尝试添加的下一个对象,并查找null .Image

答案 1 :(得分:1)

尝试使类成员,局部变量和方法参数的差异大于大小写。请参阅上面链接的规范NullReferenceException帖子中的错误命名约定

答案 2 :(得分:0)

使用此构造函数

创建每个星球
public Planet(string name, Image image, float distanceFromSun, float degreesAddedEachTick, bool clockwise, PointF rotationCenter, float angleInDegrees = 0)

然而,你永远不会检查以确保image != null && image.Width != null(如果宽度是你不需要的原生int,但如果它是Number你确实需要检查。