wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Cannot find symbol... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot find symbol..." Watch "Cannot find symbol..." New topic
Author

Cannot find symbol...

John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Hi Folks,

Got a problem that is really doing my head in.

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!

Thanks in advance!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18552
    
  40

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Hi Henry,

The action listener is called in my buildingGUI class:


The action listener class itself is:


And the error message:


Hope this helps!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18552
    
  40

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
Should I use getter and setter methods?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.,



and




[Jess in Action][AskingGoodQuestions]
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
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
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
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:


action listener:


SQL class:
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18552
    
  40

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
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
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
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!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
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
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.

When I compile, I get the error:


gList class:


btnSaveListener class:
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18552
    
  40

Have another problem know though; one that just randomly appeared. I don't know why, but it has been working.


Stuff like this don't randomly appear. If it has been working, then this is caused by something that you changed.

By chance, did you happen to change the constructor of the btnSaveListener class? Maybe added a few parameters?

Henry
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
Henry Wong wrote:
Have another problem know though; one that just randomly appeared. I don't know why, but it has been working.


Stuff like this don't randomly appear. If it has been working, then this is caused by something that you changed.

By chance, did you happen to change the constructor of the btnSaveListener class? Maybe added a few parameters?

Henry


Yep It was a bad java day yesterday!

This is the 'before' code which worked:


The 'after' code that doesn't work:


How would I get this to work?

I can't imagine it would be as simple as doing this?!:
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817



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).


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot find symbol...
 
Similar Threads
Error:cannot find getDataSource(req)
Static Doubly Linked Lists....
Having a problem with a file monitor class
Can't compile : Class not found
Application doesn't quit -- MiniMiniMusicApp sample from Head First Java