使变量成为必需的 - 以避免 NullReferenceException?

发布于 2024-08-18 13:37:00 字数 520 浏览 4 评论 0原文

[已解决] - 错误是我的,我没有将世界(world_)链接到实体,所以它是空的。谢谢大家的解释!


正如您现在可能知道的那样,我正在制作一个游戏引擎/框架,并且我一直无法将内容与彼此的引用链接起来。

示例:

public void Attach(Entity Entity)
{
    entity_ = Entity;
    entity_.world_.renderer_.AddComponent(this);
}

将组件添加到渲染器的行失败并出现 NullObjectException。 我的想法是,这是因为它位于类实现内部(当对象尚未定义时),但这样的事情在下一段代码中起作用:

public TGSGame()
{
    ...
    Renderer = new RenderManager(this);
    ...
}

这部分代码也位于 TGSGame 类实现内部!

有谁知道我怎样才能克服这个异常?

[SOLVED] - The mistake was mine, that I didn't link World (world_) to the entity, so that was null. Thanks for the explanation everyone!


As you may know by now, I'm making a game engine/framework, and I've got stuck linking stuff with references to each other.

Example:

public void Attach(Entity Entity)
{
    entity_ = Entity;
    entity_.world_.renderer_.AddComponent(this);
}

The line that adds component to the renderer fails with NullObjectException.
My idea was that it's because it's inside the class implementation (when the object isn't defined yet), but such thing worked in next piece of code:

public TGSGame()
{
    ...
    Renderer = new RenderManager(this);
    ...
}

That part of code is inside TGSGame class implementation too!

Does anyone have idea how can I overcome this exception?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

迟到的我 2024-08-25 13:37:00

您需要检查 entity_entity_.world_entity_.world_.renderer_ - 如果其中任何是< code>null 它会爆炸。如果这些是对象的基础,那么通常最好在构造函数中初始化它们,并限制它们是否可以更改/设置回 null。例如:

public class Entity {
    public World World {get;private set;}
    public Entity(World world) {
        if(world == null) throw new ArgumentNullException("world");
        tihs.World = world;
    }
}

这样做的优点是,问题出在哪里非常明显(它将显示为 "world" 参数的 ArgumentNullException,以及堆栈 -跟踪指向 World 构造函数以及调用它的任何内容,等等)

You need to check entity_, entity_.world_ and entity_.world_.renderer_ - if any of these are null it will explode. If these are fundamental to the object, it is usually a good idea to initialize them in the constructor, and limit whether they can be changed / set back to null. For example:

public class Entity {
    public World World {get;private set;}
    public Entity(World world) {
        if(world == null) throw new ArgumentNullException("world");
        tihs.World = world;
    }
}

The advantage of this is that it is very obvious where the problem is (it will show as an ArgumentNullException, of the "world" argument, with the stack-trace pointing at the World constructor and whatever is calling it, etc)

妳是的陽光 2024-08-25 13:37:00

好吧,有问题的行

entity_.world_.renderer_.AddComponent(this);

要么

entity_.world_

null ,要么

entity_.world_.renderer_

null ,或者是内部发生了一些不好的事情。

entity_.world_.renderer_.AddComponent

堆栈跟踪可以帮助我们推断出更多信息,但这些都是你的罪魁祸首。首先检查 entity_.world_ 是否已正确初始化,如果是,则 entity_world_.renderer_ 是否已正确初始化。

Well, the offending line is

entity_.world_.renderer_.AddComponent(this);

so either

entity_.world_

is null or

entity_.world_.renderer_

is null or something bad is happening inside of

entity_.world_.renderer_.AddComponent

A stack trace would help us deduce a little more but those are your culprits. Start by checking if entity_.world_ is being initialized properly and if so if entity_world_.renderer_ is being initialized properly.

凉风有信 2024-08-25 13:37:00

entity_.world_.renderer_ 链中的一个引用为 null,这并不奇怪,因为您刚刚在上一行中创建了它。请注意,让一个对象以这种方式将自身添加到外部集合中是一种奇怪的责任倒置——通常,控制集合的人应该控制向集合中添加内容的人。

One of the references in the chain of entity_.world_.renderer_ is null, which isn't surprising, since you just created it in the previous line. Note that having an object add itself that way to an external collection is a bizarre responsibility inversion -- usually whoever is controlling the collection should get control over who adds things to it.

迷你仙 2024-08-25 13:37:00

我猜想 world_renderer_ 引用成员尚未初始化。该实体或 entity_ 作为 null 传入。如果没有更多关于它们是什么的信息,很难说清楚。

I'd guess either world_ or renderer_ reference members which have not yet been initialised. Either that or entity_ is passed in as a null. It's a bit hard to tell without more info on what they are though.

听,心雨的声音 2024-08-25 13:37:00

您需要确保在调用 Attach 之前“world_”和“renderer_”都存在。

您可以使用防御性代码:

if ((entity_ != null) && (entity_.world_ != null) && (renderer_ != null)) {
   //... your code here...
} else {
   throw new Exception("...");
}

除此之外,您还需要查看如何构建对象图以确保不会出现这种情况。 FactorySingleton 模式在这种情况下可以提供帮助。

You need to ensure that "world_" and "renderer_" both exist before the call to Attach.

You could use defensive code:

if ((entity_ != null) && (entity_.world_ != null) && (renderer_ != null)) {
   //... your code here...
} else {
   throw new Exception("...");
}

Beyond that, you need to look at how you build your object graph to ensure that this situation does not arise. A good combination of Factory and Singleton pattern can help in this case.

ぽ尐不点ル 2024-08-25 13:37:00

也许您有 NullReferenceException?无论如何,我会仔细检查既不是

entity_.world_

也不

entity_.world_.renderer_

为空。只需放置一个断点,您就会看到您的 Entity 参数尚未完全初始化。

Perhaps you had NullReferenceException? Anyway, I would double-check that neither

entity_.world_

nor

entity_.world_.renderer_

is not null. Just place a breakpoint and you'll see that your Entity argument isn't completely initialized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文