为什么我在这里得到NullReferenceException

本文关键字:NullReferenceException 在这里 为什么 | 更新日期: 2023-09-27 18:19:25

我有:

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时设置图像时会出现异常。

EDIT:以下是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:我发现我的问题了,但是我得睡觉了,我明天会把答案贴出来。

为什么我在这里得到NullReferenceException

看起来你从来没有实例化你的'Planets'对象(可能是List<Planet> ?)

是在你的StarSystem代码?

大概应该是

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

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

Post Question编辑:好吧,那么唯一剩下的似乎就是一个或多个行星的.Image了。在调试时,您应该能够监视变量—我会在它求值表达式之前设置一个断点,并查看它添加的最后一个对象,以及它将尝试添加的下一个对象,并查找null .Image

尽量让类成员、局部变量和方法参数的区别大于大写。请参阅上面链接的NullReferenceException帖子中的错误命名约定

使用这个构造函数创建每个行星

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

然而,你从来没有检查以确保image != null && image.Width != null(如果宽度是一个本地int,你不需要,但如果它是一个Number,你确实需要检查。