Results 1 to 3 of 3
  1. #1
    Soda is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

    Default Whats wrong with my code???

    public void actionPerformed(ActionEvent ae)
    {
    String name, age;
    String location;
    String gender, conduct, gift;

    //rbMale
    if(ae.getSource() == rbMale)
    {
    gender = "Male";
    }

    //rbFemale
    if(ae.getSource() == rbFemale)
    {
    gender = "Female";
    }

    //rbNice
    if(ae.getSource()== rbNice)
    {
    cbGiftNice.setVisible(true);
    cbGiftNaughty.setVisible(false);
    conduct = "Nice";

    if(ae.getSource() == cbGiftNice)
    {
    JComboBox cbNice = (JComboBox)ae.getSource();
    gift = (String)cbGiftNice.getSelectedItem();
    }
    }

    //rbNaughty
    if(ae.getSource() == rbNaughty)
    {
    cbGiftNaughty.setVisible(true);
    cbGiftNice.setVisible(false);
    conduct = "Naughty";

    if(ae.getSource() == cbGiftNaughty)
    {
    JComboBox cbNaughty = (JComboBox)ae.getSource();
    gift = (String)cbGiftNaughty.getSelectedItem();
    }
    }

    //cbLocation
    if(ae.getSource() == cbLocation)
    {
    JComboBox cbLocation = (JComboBox)ae.getSource();
    location = (String)cbLocation.getSelectedItem();
    }

    //bView
    if(ae.getSource() == bView)
    {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame vFrame = new JFrame("List");
    vFrame.setSize(300,800);
    vFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    vFrame.setVisible(true);

    JPanel vPanel = new JPanel();
    JTextArea taView = new JTextArea();

    vFrame.add(vPanel);
    vPanel.add(taView);

    name = tfName.getText();
    age = tfAge.getText();

    taView.append("Name:"+(" ")+name);
    taView.append("Age:"+(" ")+age);
    taView.append("Sex:"+(" ")+gender);
    taView.append("Conduct:"+(" ")+conduct);
    taView.append("Location:"+(" ")+location);
    taView.append("Gift:"+(" ")+gift);
    }
    Here the problem:

    The last variables gender, conduct, location and gift, in taView.append, gets an error during compilation. The error was "variable gender might not have been initialized" etc, but name and age does not get any error. If add the declaration in the bView, the error becomes something like this, "error has been declared at ActionEvent" Why is that? Whats the error in my code?

    Thnx much for the help!!!
    Last edited by Soda; 12-05-2007 at 02:27 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Logic. Consider this:
    Java Code:
    public void actionPerformed(ActionEvent ae)
    {
        String name, age;
        String location;
        String gender, conduct, gift;
    
        //rbMale
        if(ae.getSource() == rbMale)
        {
            gender = "Male";
        }
    
        //rbFemale
        if(ae.getSource() == rbFemale)
        {
            gender = "Female";
        }
        ...
        //bView
        if(ae.getSource() == bView)
        {
            ...
            name = tfName.getText();
            age = tfAge.getText();
    
            taView.append("Name:"+(" ")+name); 
            taView.append("Age:"+(" ")+age); 
            taView.append("Sex:"+(" ")+gender);
            ...
        }
    }
    The only way that the String variable "gender" can/will be assigned a value is if ae.getSource() == either "rbMale" or "rbFemale". In any other case, most notably here: "bView", the local variable "gender" has no value assigned to it. The compiler needs to see a value for "gender" before you try to use it.
    Solution: give it a value (initialize it) at declaration, eg:
    Java Code:
        String gender = "unknown";

  3. #3
    Soda is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

Similar Threads

  1. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 09:27 AM
  2. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 08:57 PM
  3. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-23-2008, 12:55 AM
  4. Is there somethign wrong with this code?
    By Soda in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 05:46 PM
  5. whats the difference between Java core,J2EE......
    By prince24 in forum New To Java
    Replies: 2
    Last Post: 07-11-2007, 07:54 PM

Posting Permissions

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