I have a class that builds the GUI for my app in one file and in another file I have an action listener class.
In the action listener class, I want to get the text from each field and assign it to a variable as so:
The problem I'm having is that I get the 'cannot find symbol' error.
It's all in the same package and has package level access, so I'm really confused!
There are numerous reasons why the compiler can't resolve a symbol -- you need to provide more info here. Maybe you could show us the exact error message. Or even show some of the code.
Basically, you code references the symbol tfName at various places...
And the compiler can't resolve it. It isn't a local variable. It isn't an instance or class variable. It isn't a classname. etc. There is nothing in scope that the compiler can find that is tfName.
Henry
John Pisci
Ranch Hand
Joined: Dec 19, 2008
Posts: 44
posted
0
Should I use getter and setter methods?
Ernest Friedman-Hill
author and iconoclast
Marshal
John Pisci wrote:Should I use getter and setter methods?
Maybe, maybe not. But the most important thing you need to do is declare the variables someplace. Maybe you're already doing that. Are they member variables declared in your "buildingGUI" class, or some other class? Then your btnSaveListener needs to have a reference to the object that contains the variables; i.e.,
The variables are defined in the class gList (which contains the main method and buildingGUI method).
Should I reference the the object 'g' when creating btnSaveListener as that is the object of the class 'gList' where all the variables are initially declared?
As in:
Also, in your example, you've mentioned the following, but I don't quite understand it (sorry- I know I'm being a bit thick!):
Here's my gList class, with my main method and buildingGUI method:
John Pisci
Ranch Hand
Joined: Dec 19, 2008
Posts: 44
posted
0
Right, think I've got it sorted, thanks for all of your help!
Added to the buildingGUI method:
Added to the btnSaveListener class
John Pisci
Ranch Hand
Joined: Dec 19, 2008
Posts: 44
posted
0
Thought I'd may as well just bump this thread instead of creating a new one as it is a similar problem.
Anyway, I have an action listener for a button that gets all of the info in the fields of the GUI and assigns them to variable. This then calls an SQL method which performs the SQL insert.
I am having trouble with getting the variables from the action listener into the SQL insert method.
Thanks in advance!
I get the following error for each and everyone of the variables:
I get the following error for each and everyone of the variables:
Each and every one of those variables are *local* variables of the actionPerformed() method. They are not in scope outside of the method.
Henry
Fred Hamilton
Ranch Hand
Joined: May 13, 2009
Posts: 679
posted
0
Henry Wong wrote:
I get the following error for each and everyone of the variables:
Each and every one of those variables are *local* variables of the actionPerformed() method. They are not in scope outside of the method.
Henry
It seems to me there some questions of programming style here also?
We have separate class for just an action listener and little else. (btnSaveListener) And that class is doing a lot of work with a lot of different variables. It seems that is a scenario that invites trouble. Sure you can make it work, but...
Maybe there are reasons for doing it this way, I don't know. There's always different ways of doing things. But I like to keep my action listeners much cleaner than what is shown here, and rely more on appropriate method calls.
John Pisci
Ranch Hand
Joined: Dec 19, 2008
Posts: 44
posted
0
Henry Wong wrote:
I get the following error for each and everyone of the variables:
Each and every one of those variables are *local* variables of the actionPerformed() method. They are not in scope outside of the method.
Henry
As soon as I read this, I realised what I was doing wrong! Thanks!
I have now declared the variables outside of the actionPerformed() method.
Now on to the next set of errors...
John Pisci
Ranch Hand
Joined: Dec 19, 2008
Posts: 44
posted
0
Fred Hamilton wrote:
Henry Wong wrote:
I get the following error for each and everyone of the variables:
Each and every one of those variables are *local* variables of the actionPerformed() method. They are not in scope outside of the method.
Henry
It seems to me there some questions of programming style here also?
We have separate class for just an action listener and little else. (btnSaveListener) And that class is doing a lot of work with a lot of different variables. It seems that is a scenario that invites trouble. Sure you can make it work, but...
Maybe there are reasons for doing it this way, I don't know. There's always different ways of doing things. But I like to keep my action listeners much cleaner than what is shown here, and rely more on appropriate method calls.
I hadn't thought about just using the action listener to perform method calls.
Will the performance be affected doing it the way I am? Or is it just a personal preference to have clean action listeners?
As you can tell, I am learning, but I appreciate all of the advice I can get!
It's not an issue of performance, it's an issue of readability and maintainability.
Fred Hamilton
Ranch Hand
Joined: May 13, 2009
Posts: 679
posted
0
exactly. Readability and maintainability. And in addition to considerations about how much work you want your event handler (actionPerformed) to do, also one can think about the degree to which the application is "classified".
Just seeing this one class which is little more than an event handler leads to believe that you might end up with a real proliferation of classes. If you wanted to be able to use this particular listener/event handler with other applications, then your idea is pretty good. If the listener has no use outside of the current application, then maybe it's not so good.
In my programs, I usually have a main GUI class that includes most if not all of my listeners and event handlers, and the event handlers themselves are very minimal, often just a method call, and the method is in another class. This approach serves to separate functionality from presentation.
Again, this sort of stuff is more a question of style, readability, maintainability. You know your program best, so it's for you to decide these things, I'm not saying what is best, just pointing out a few design decisions you need to make. I haven't done much database work with java, so maybe that influences general application design.
John Pisci
Ranch Hand
Joined: Dec 19, 2008
Posts: 44
posted
0
Fred Hamilton wrote:exactly. Readability and maintainability. And in addition to considerations about how much work you want your event handler (actionPerformed) to do, also one can think about the degree to which the application is "classified".
Just seeing this one class which is little more than an event handler leads to believe that you might end up with a real proliferation of classes. If you wanted to be able to use this particular listener/event handler with other applications, then your idea is pretty good. If the listener has no use outside of the current application, then maybe it's not so good.
In my programs, I usually have a main GUI class that includes most if not all of my listeners and event handlers, and the event handlers themselves are very minimal, often just a method call, and the method is in another class. This approach serves to separate functionality from presentation.
Again, this sort of stuff is more a question of style, readability, maintainability. You know your program best, so it's for you to decide these things, I'm not saying what is best, just pointing out a few design decisions you need to make. I haven't done much database work with java, so maybe that influences general application design.
That makes a lot of sense, I'd not really thought about doing it that way.
This is my first java app. with a GUI and a database and to be honest, I think I've made quite a few design mistakes. Saying that though, it is the best way to learn.
Have another problem know though; one that just randomly appeared. I don't know why, but it has been working.
This constructor accept three different types of arguments (gList,connect,SQLShizzle) . So you need to call that constructor passing instances which are compatible with those types. (Passing "this" for all three types would work only if the current instance which you are calling the constructor is assignable for all those three types, which is not in this case).