View Single Post
  #2 (permalink)  
Old 12-05-2007, 08:39 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
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";
Reply With Quote