Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-05-2007, 02:24 PM
Member
 
Join Date: Dec 2007
Posts: 15
Soda is on a distinguished road
Whats wrong with my code???
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-05-2007, 08:39 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Logic. Consider this:
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:
Code:
String gender = "unknown";
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-06-2007, 01:54 PM
Member
 
Join Date: Dec 2007
Posts: 15
Soda is on a distinguished road
Thnx much! It working now!!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 07:46 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org