简单的空引用错误让我丧命(Java)
好吧,我在 java 中搞乱了一些代码,并且收到了一个奇怪的错误。我有我的混沌类,它也有一个公共的窗口变量 FSW。现在我有另一个类叫做 Look。 Chaos 创建一个 Look,然后运行 Look.Init() 方法。该 init 方法运行looks run 方法,该方法尝试引用其父 Chaos 的 FSW 变量。
问题是,无论我如何解决这个问题,每当我从 Look 中引用 Chaos 中的 -any- 变量时,变量都是 null =/。我可以从子类 Look 调用 Chaos 方法,但无法引用变量。
这里是一个文本托管网站的链接,如果有人认为我有必要导出和上传包,我想我会的,但我觉得这可能只是我没有看到的明显的东西。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题是你实际上没有从 Chaos 中引用变量,而是从 Look 中引用变量。
即你用它自己的 FSW 实例创建一个新的 Look() 对象,默认情况下初始化为 null,这永远不会在 Look 中设置。
如果你想在 Chaos 中引用该变量,我建议你将 Chaos 对象传递到 Look 的构造函数中。
因此,在 Look 中,您将放置一个新字段 Chaos,并添加一个构造函数,如下所示
Inside Chaos 在创建 Look 时,您将执行
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
Inside Chaos when creating Look you would then do
Inside look you could then reference chaos.FSW