• Post Reply Bookmark Topic Watch Topic
  • New Topic

ActionListener: Non-static variable 'this' cannot be referenced from a non-static context????  RSS feed

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! I'm not sure why the below code is giving me the error message: 'non-static variable this cannot be referenced from a non-static context', which points to tl, my TimerListener, which implements ActionListener. Is 'this' implied somewhere there that I'm not seeing? I'm not sure what 'this' would have to do with my TimerListener??? I've Googled extensively and can't really find any explanation in my text. Does anyone have any ideas or an explanation? If need be, I can post my other classes, but I don't think that's the issue here. Thanks in advance!

 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello it looks to me that your action listener (TimeListener) "should" be a inner class and you haven't instantiate the CarMove class - that's why you get the error.

Either make TimeListener class static or new CarMover().new TimeListener()

This behavior is the same as calling a customed method inside main like this:

In the above code you would get the same error. Hope this helps.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Hello it looks to me that your action listener (TimeListener) "should" be a inner class and you haven't instantiate the CarMove class - that's why you get the error.

Either make TimeListener class static or new CarMover().new TimeListener()

This behavior is the same as calling a customed method inside main like this:


That did it, thank you kindly!
In the above code you would get the same error. Hope this helps.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would strongly suggest you re-write this code from scratch and get rid of static everything. The only thing that absolutely needs to be static is the main method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!