使变量成为必需的 - 以避免 NullReferenceException?
[已解决] - 错误是我的,我没有将世界(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要检查
entity_
、entity_.world_
和entity_.world_.renderer_
- 如果其中任何是< code>null 它会爆炸。如果这些是对象的基础,那么通常最好在构造函数中初始化它们,并限制它们是否可以更改/设置回 null。例如:这样做的优点是,问题出在哪里非常明显(它将显示为
"world"
参数的ArgumentNullException
,以及堆栈 -跟踪指向World
构造函数以及调用它的任何内容,等等)You need to check
entity_
,entity_.world_
andentity_.world_.renderer_
- if any of these arenull
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: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 theWorld
constructor and whatever is calling it, etc)好吧,有问题的行
要么
是
null
,要么是
null
,或者是内部发生了一些不好的事情。堆栈跟踪可以帮助我们推断出更多信息,但这些都是你的罪魁祸首。首先检查
entity_.world_
是否已正确初始化,如果是,则entity_world_.renderer_
是否已正确初始化。Well, the offending line is
so either
is
null
oris
null
or something bad is happening inside ofA 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 ifentity_world_.renderer_
is being initialized properly.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.我猜想
world_
或renderer_
引用成员尚未初始化。该实体或entity_
作为null
传入。如果没有更多关于它们是什么的信息,很难说清楚。I'd guess either
world_
orrenderer_
reference members which have not yet been initialised. Either that orentity_
is passed in as anull
. It's a bit hard to tell without more info on what they are though.您需要确保在调用 Attach 之前“world_”和“renderer_”都存在。
您可以使用防御性代码:
除此之外,您还需要查看如何构建对象图以确保不会出现这种情况。 Factory 和 Singleton 模式在这种情况下可以提供帮助。
You need to ensure that "world_" and "renderer_" both exist before the call to Attach.
You could use defensive code:
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.
也许您有 NullReferenceException?无论如何,我会仔细检查既不是
也不
为空。只需放置一个断点,您就会看到您的 Entity 参数尚未完全初始化。
Perhaps you had NullReferenceException? Anyway, I would double-check that neither
nor
is not null. Just place a breakpoint and you'll see that your Entity argument isn't completely initialized.