Results 1 to 7 of 7
  1. #1
    ayershov777 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Cannot find symbol: This is driving me insane!

    Hey all, I am getting a compile error: Cannot find symbol. Can anybody please help me resolve this issue?

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class TFTest extends JFrame
    {
    	public TFTest()
    	{
    		JTextField tf = new JTextField("0",5);
    		Listener listener = new Listener();
    		
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		tf.addActionListener(listener);
    		add(tf);
    		pack();
    		setVisible(true);
    	}
    	
    	public class Listener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			tf.setText("It worked!");
    		}
    	}
    }
    Thanks

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Cannot find symbol: This is driving me insane!

    Which symbol is the compiler message talking about? And which line of your code does it refer to?

    Basically, "cannot find symbol" occurs whenever you use an identifier (eg method or variable) and the compiler has no clue what you are talking about. This can be because of a typo (misspelling) or, in the case of methods, because you are not supplying the right number and types of arguments. With variables the compiler will complain if the variable is not "in scope": what this means is that the variable must be declared in the same {} block as you use it, or in an enclosing one.

  3. #3
    ayershov777 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: Cannot find symbol: This is driving me insane!

    The error tells me that tf is not found in the ActionListener class

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Cannot find symbol: This is driving me insane!

    Then it would appear that tf (which was declared in the TFTest constructor) in not in scope once you get to the actionPerformed() method of the Listener class.

  5. #5
    ayershov777 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: Cannot find symbol: This is driving me insane!

    and how do I go about fixing that?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Cannot find symbol: This is driving me insane!

    how do I go about fixing that?
    You declare tf so that its scope includes the actionPerformed() method.

    Did you understand what I meant by "scope"?

  7. #7
    ayershov777 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: Cannot find symbol: This is driving me insane!

    ahhh okay i understand thanks =)

Similar Threads

  1. Cannot find symbol
    By dest in forum New To Java
    Replies: 3
    Last Post: 04-03-2012, 05:38 PM
  2. Cannot find Symbol
    By webbby in forum New To Java
    Replies: 3
    Last Post: 04-14-2011, 12:51 AM
  3. Replies: 1
    Last Post: 08-27-2009, 01:20 PM
  4. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  5. Replies: 3
    Last Post: 05-01-2008, 08:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •