Results 1 to 3 of 3
  1. #1
    Andyj is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default Cannot find symbol problem

    Hi there

    I'm fairly confused about a compiler error I keep getting with the following code (planning to tidy up the import statments later). The problem is the last two lines of code, which give me the message: cannot find symbol, variable tomandy (or variable Tom), class Party.

    I've tried putting them inside the main method which doesn't complie either, and I can't put them in another class since I need to delcare them in the class that declares ActionListener...

    What I'm trying to do is get the window to run the Halsey2.stuff method refresh the window when you click the JButton. It works fine if I leave the button with no function.

    Would be massively grateful for any advice? Thanks!


    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import static java.lang.System.out;
    import javax.swing.*;

    public class Party implements ActionListener{

    public static void main(String[] args){
    Halsey2 Tomandy = new Halsey2();
    Tomandy.stuff();

    JLabel one = new JLabel ("Salutations. I would like to invite you to a very special

    party.", SwingConstants.CENTER);
    JLabel two = new JLabel ("We will partake in the following:",SwingConstants.CENTER);
    JLabel three = new JLabel ("Listening to " + Tomandy.music, SwingConstants.CENTER);
    JLabel four = new JLabel ("Drinking " + Tomandy.drink, SwingConstants.CENTER);
    JLabel five = new JLabel ("Eating " + Tomandy.food, SwingConstants.CENTER);
    JLabel six = new JLabel ("The dress code is " + Tomandy.clothes, SwingConstants.CENTER);
    JLabel seven = new JLabel ("I do hope you will attend.", SwingConstants.CENTER);
    JButton eight = new JButton("No thanks, try again.");
    eight.addActionListener(Tomandy);

    JPanel pane = new JPanel();
    GridLayout airmax = new GridLayout (8,1);
    pane.setLayout(airmax);
    pane.add(one);
    pane.add(two);
    pane.add(three);
    pane.add(four);
    pane.add(five);
    pane.add(six);
    pane.add(seven);
    pane.add(eight);

    JFrame Tom = new JFrame("Party of possible doom");
    Tom.add(pane);
    Tom.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    Tom.pack();
    Tom.setVisible(true);}


    public void actionPerformed(ActionEvent event) {
    Tomandy.stuff();
    Tom.repaint();
    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You have a scope issue here:

    Java Code:
    public class Party implements ActionListener {
    
      public static void main(String[] args) {
        Halsey2 Tomandy = new Halsey2();
        //....
    
        JFrame Tom = new JFrame("Party of possible doom");
        //....
      }
    
      public void actionPerformed(ActionEvent event) {
        Tomandy.stuff();
        Tom.repaint();
      }
    
    }
    The variables you are trying to access have been declared inside of main and are visible inside main only. I think that you may want to rethink everything so that your design is more OOP-natured. You'll likely want to make your main much simpler so that it perhaps initializes the app and that's it.

    Also, please read the link in my signature regarding code tags.

    Best of luck!

  3. #3
    Andyj is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Cannot find symbol
    By SarahB in forum New To Java
    Replies: 0
    Last Post: 03-06-2010, 03:03 PM
  2. Can not find symbol ???
    By AliceNewbie in forum New To Java
    Replies: 1
    Last Post: 02-17-2010, 01:44 AM
  3. cannot find symbol
    By GabWit in forum New To Java
    Replies: 3
    Last Post: 01-25-2009, 12:13 AM
  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
  •