• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Removing Listener: "variable mouseListener might not have been initialized"

 
Ranch Foreman
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Upon initial compiling, I receive the error: "variable mouseListener might not have been initialized"
I choose the available option "initiatialize variable mouseListener"

After compiling, I receive the error message "cannot assign a value to final variable mouseListener"

Last evening while working on this I researched and came across a way to accomplish my goal. I am using what I see as a reference.

The following snippet demonstrates creating this reference to mouseListener:



At this time, after compile, I'm receiving an error message that using = null is not permitted (which appears to send me into a sort of circle, hence the apparent need for a reference.)

(The primary goal is to remove a mouselistener in order to simulate mouse freeze but first I need to figure this out. Thank you kindly for any nudge in the correct direction.)
 
Marshal
Posts: 6207
501
IntelliJ IDE Python TypeScript Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need to reassign different objects to a variable then it cannot be final. By declaring your variable as final you are telling the compiler that once it has been assigned an object then you have no intention of ever changing your mind and you want the compiler to forbid it.

In your case here the final keyword behaviour is in opposition to your intended design so you'll want to remove it.
 
Mat Dif
Ranch Foreman
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:If you need to reassign different objects to a variable then it cannot be final. By declaring your variable as final you are telling the compiler that once it has been assigned an object then you have no intention of ever changing your mind and you want the compiler to forbid it.

In your case here the final keyword behaviour is in opposition to your intended design so you'll want to remove it.



When I remove Final, I receive the error "local variables referenced from an inner class must be final or effectively final"
 
Tim Cooke
Marshal
Posts: 6207
501
IntelliJ IDE Python TypeScript Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah well in that case your intended design cannot be brought into reality. Instead you could implement a "freezable mouse listener" or something similar, where the listener code checks whether it's in a freeze or not and responds accordingly.

Declaring and assigning the final variable at the same time will resolve your problems related to variable assignment. E.g:
 
Tim Cooke
Marshal
Posts: 6207
501
IntelliJ IDE Python TypeScript Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the implementation of the FreezableMouseListener, you got some solid advice in your other topic a few days ago that should put you in the right direction with it.
 
Mat Dif
Ranch Foreman
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:On the implementation of the FreezableMouseListener, you got some solid advice in your other topic a few days ago that should put you in the right direction with it.



I greatly appreciate the assistance and while busy attempting to  implement this, new issues have cropped up and while in the same area of the project, the answers seem entirely different. Thanks again.
 
Saloon Keeper
Posts: 29001
214
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a sneaking suspicion that this code would be better implemented as a lambda these days.
 
Bartender
Posts: 5751
217
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MouseListener and MouseMotionListener are not Functional Interfaces, so you cannot use lambda's here.

@Mat
If you use (see lines 1 and 2 in your opening post)

then, for the compiler, mouseListener is a MouseListener. And so you cannot directly do:

because addMouseMotionListener requires a MouseMotionListener, and not a MouseListener.
A simple remedy would be what is called a CAST  (transforming for a moment from one type to another type)

But much easier is it to declare:

and then in the constructor:

And since a MouseAdapter is both a MouseListener and a MouseMotionListener, you can simply do:

This is what I did in the code in the previous topic, that Tim referred to in his post.

 
Mat Dif
Ranch Foreman
Posts: 43
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, working on this right now. Appreciate it.
 
Marshal
Posts: 81613
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please avoid quoting the whole of the preceding post. That adds nothing and is liable to removal.
 
It's a tiny ad only because the water is so cold.
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic