-2
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Project2KarlAnderson extends JFrame 
{

     private JTextField firstName = new JTextField();
     private JTextField lastName = new JTextField();
     private JTextField age = new JTextField();
     private JRadioButton mGender = new JRadioButton("Male");
     private JRadioButton fGender = new JRadioButton("Female");

     private JButton printName = new JButton("Print Name");


    public Project2KarlAnderson()
    {

     JPanel panelOne = new JPanel(new GridLayout(3, 3, 5, 5));
      panelOne.add(new JLabel("Enter First Name"));
      panelOne.add(firstName);
      panelOne.add(new JLabel("Enter Last Name"));
      panelOne.add(lastName);
      panelOne.add(new JLabel("Enter Age"));
      panelOne.add(age);


     JPanel panelTwo = new JPanel(new FlowLayout(FlowLayout.RIGHT));
       panelTwo.add(printName);


     JPanel panelThree = new JPanel(new FlowLayout(FlowLayout.LEFT));
      panelThree.add(mGender);
      panelThree.add(fGender);

     String[] states = {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", 
     "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", 
         "MA", "MI", "MN", "MS", "MO","MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH",
         "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI",
         "WY"};

     JComboBox stateList = new JComboBox(states);






     JPanel panelFour = new JPanel(new FlowLayout(FlowLayout.RIGHT));
     panelFour.add(stateList);

     add(panelOne, BorderLayout.CENTER);
     add(panelTwo, BorderLayout.SOUTH);
     add(panelThree, BorderLayout.WEST);
     add(panelFour, BorderLayout.EAST);


     printName.addActionListener(new ButtonListener());
     }

     private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            String first = firstName.getText();
            String last = lastName.getText();           
            String state = stateList.getSelectedText().toString();

            System.out.print("Welcome "+ first +" " + last +" " + state);

        }       

    }   


    public static void main(String[] args)
    {
        Project2KarlAnderson form = new Project2KarlAnderson();
            form.setTitle("Name Box");
            form.setSize(350, 175);
            form.setLocationRelativeTo(null);
            form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            form.setVisible(true);
    }
}

I get the following errors. I have been racking my brain trying to figure out whats wrong.

java:68: error: cannot find symbol
                String state = stateList.getSelectedText().toString();
                               ^
symbol:   variable stateList
location: class Project2KarlAnderson.ButtonListener

Note: Project2KarlAnderson.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 2
    Do you know what "cannot find symbol" error means? – Qiu Feb 15 '14 at 23:17
  • 1
    `stateList` seems to be local variable declared in constructor. Consider moving it to your class level and making it field if you want to use it in other methods, or pass it as method argument. – Pshemo Feb 15 '14 at 23:17
  • You dont have `stateList` variable declared, or it isn't visible from current scope. – kajacx Feb 15 '14 at 23:18
  • possible duplicate of [Cannot find Symbol: Java](http://stackoverflow.com/questions/12462057/cannot-find-symbol-java) – Dennis Meng Feb 15 '14 at 23:35
  • I changed stateList.getSelectedText().toString(); to stateList.getSelectedItem().toString(); everything works fine now. – user3314682 Feb 16 '14 at 06:17

2 Answers2

0

The field stateList is declared inside constructor. So it's not available outside. It's a local variable.
To fix this problem, you need to declare variable outside constructor. For example eclare it after line "JButton printName". But you can declare it with null at start. The you can assign a value (reference) to this variable in constructor like you have done.

apocalypse
  • 5,764
  • 9
  • 47
  • 95
  • If I take this line out "String state = stateList.getSelectedText().toString();" and change it to String state = "text"; it compiles fine and I just get the error "Note: Project2KarlAnderson.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. – user3314682 Feb 16 '14 at 00:46
0

Ok do this,

public class Project2KarlAnderson extends JFrame 
{
    String[] states = {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", 
                  "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME",  "MD", 
 "MA", "MI", "MN", "MS", "MO","MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND","OH",
 "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI",
 "WY"};

JComboBox stateList;

public Project2KarlAnderson()
{
     stateList = new JComboBox(states);
}}
Kuru Kshetran
  • 367
  • 1
  • 4
  • 10
  • Project2KarlAnderson.java:68: error: cannot find symbol String state = stateList.getSelectedText().toString(); ^ symbol: method getSelectedText() location: variable stateList of type JComboBox – user3314682 Feb 16 '14 at 00:39