开发者

Simple null reference bug thats killing me (Java)

开发者 https://www.devze.com 2023-01-30 07:41 出处:网络
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

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 can开发者_如何学编程t 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号