Results 1 to 3 of 3
Thread: Whats wrong with my code???
- 12-05-2007, 01:24 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 15
- Rep Power
- 0
Whats wrong with my code???
Here the problem: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);
}
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 01:27 PM.
- 12-05-2007, 07:39 PM #2
Logic. Consider this:
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.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); ... } }
Solution: give it a value (initialize it) at declaration, eg:
Java Code:String gender = "unknown";
- 12-06-2007, 12:54 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
what is wrong with this code
By masaka in forum New To JavaReplies: 5Last Post: 04-16-2008, 08:27 AM -
Cannot understand whats wrong
By Lehane_9 in forum New To JavaReplies: 1Last Post: 03-06-2008, 07:57 PM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM -
Is there somethign wrong with this code?
By Soda in forum New To JavaReplies: 1Last Post: 12-08-2007, 04:46 PM -
whats the difference between Java core,J2EE......
By prince24 in forum New To JavaReplies: 2Last Post: 07-11-2007, 06:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks