简单的空引用错误让我丧命(Java)

发布于 2024-10-07 15:36:16 字数 465 浏览 0 评论 0原文

好吧,我在 java 中搞乱了一些代码,并且收到了一个奇怪的错误。我有我的混沌类,它也有一个公共的窗口变量 FSW。现在我有另一个类叫做 Look。 Chaos 创建一个 Look,然后运行 ​​Look.Init() 方法。该 init 方法运行looks run 方法,该方法尝试引用其父 Chaos 的 FSW 变量。

问题是,无论我如何解决这个问题,每当我从 Look 中引用 Chaos 中的 -any- 变量时,变量都是 null =/。我可以从子类 Look 调用 Chaos 方法,但无法引用变量。

这里是一个文本托管网站的链接,如果有人认为我有必要导出和上传包,我想我会的,但我觉得这可能只是我没有看到的明显的东西。

http://www.text-upload.com/read.php?t=1790

Alright so I am messing around with some code in java and I am getting a wierd error. I have my class Chaos which has a Window variable FSW, public as well. Now I have another class called Look. Chaos creates a Look and then runs the Look.Init() method. That init method runs the looks run method which tries to reference the FSW variable of its parent Chaos.

The problem is that no matter how I got about it whenever I reference -any- variable from Chaos from within Look the variable is null =/. I can call Chaos methods from the subclass Look but I cant reference variables.

Here is a link to a text hosting site, if anyone thinks it would be necessary for me to export and upload the package I guess I will but I feel like this might be just something I am not seeing that is obvious.

http://www.text-upload.com/read.php?t=1790

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

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

发布评论

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

评论(1

分開簡單 2024-10-14 15:36:16

你的问题是你实际上没有从 Chaos 中引用变量,而是从 Look 中引用变量。

即你用它自己的 FSW 实例创建一个新的 Look() 对象,默认情况下初始化为 null,这永远不会在 Look 中设置。

如果你想在 Chaos 中引用该变量,我建议你将 Chaos 对象传递到 Look 的构造函数中。

因此,在 Look 中,您将放置一个新字段 Chaos,并添加一个构造函数,如下所示

public Look(Chaos chaos){
   this.chaos = chaos
}

Inside Chaos 在创建 Look 时,您将执行

new Look(this)

Inside Look,然后您可以引用 Chaos.FSW

Your problem is your not actually referencing a variable from within Chaos, your referencing a variable from within Look.

i.e. you create a new Look() object with it's own instance of FSW which by default is initalised to null, this is never set inside Look

If you want to reference the variable in Chaos I suggest you pass the Chaos object into the Look's constructor.

So in look you would put a new field chaos, and add a constructor like so

public Look(Chaos chaos){
   this.chaos = chaos
}

Inside Chaos when creating Look you would then do

new Look(this)

Inside look you could then reference chaos.FSW

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