我试图用动作侦听器开发一个简单的挥杆应用程序。我使用了JComboBox。它有一个动作监听器。当我对其执行某些操作并单击“保存”按钮时,出现异常。当我检查组合框中获得的值时,该值是正确的。但是仍然出现空指针异常。
代码:
package swings;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class Survey extends JFrame {
JFrame jframe = null;
JTextField name;
JTextArea address, hobbies, describe;
JButton save, cancel;
JLabel nameLabel, cityLabel, countryLabel, addressLabel, hobbiesLabel, describeLabel;
JComboBox city=null, country=null;
String[] citiesIndia = { "Hyderabad", "Kadapa", "Ranchi", "Vizag", "Delhi", "Pune", "Bangalore"};
String[] citiesPakistan = {"Lahore", "Rawalpindi"};
String[] citiesUS = {"LA", "New York", "Cancass City", "LaPito"};
String[] countryList = {"India", "Pakistan", "USA"};
private void clearAllOPtions() {
name.setText("");
country.setSelectedItem(null);
city.setSelectedItem(null);
System.out.println(country.getSelectedItem());
city.setEnabled(false);
address.setText("");
hobbies.setText("");
describe.setText("");
}
public Survey() {
this.jframe = new JFrame("Survey");
this.nameLabel = new JLabel("Name : ");
this.name = new JTextField();
this.cityLabel = new JLabel("City : ");
this.city = new JComboBox<String>();
this.city.setSelectedItem(null);
this.city.setEnabled(false);
this.countryLabel = new JLabel("Country : ");
this.country = new JComboBox<String>(countryList);
this.country.setSelectedItem(null);
this.addressLabel = new JLabel("Address : ");
this.address = new JTextArea();
this.hobbiesLabel = new JLabel("Hobbies : ");
this.hobbies = new JTextArea();
this.describeLabel = new JLabel("Describe : ");
this.describe = new JTextArea();
this.save = new JButton(new ImageIcon("save.png"));
this.cancel = new JButton(new ImageIcon("cancel.png"));
jframe.add(nameLabel);
nameLabel.setBounds(50, 100, 70, 30);
jframe.add(name);
name.setBounds(150, 100, 150, 30);
jframe.add(cityLabel);
countryLabel.setBounds(50, 140, 70, 30);
jframe.add(city);
country.setBounds(150, 140, 150, 30);
jframe.add(countryLabel);
cityLabel.setBounds(50, 180, 70, 30);
jframe.add(country);
city.setBounds(150, 180, 150, 30);
jframe.add(addressLabel);
addressLabel.setBounds(50, 220, 70, 30);
jframe.add(address);
address.setBounds(150, 220, 150, 30);
jframe.add(hobbiesLabel);
hobbiesLabel.setBounds(50, 260, 70, 30);
jframe.add(hobbies);
hobbies.setBounds(150, 260, 150, 30);
jframe.add(describeLabel);
describeLabel.setBounds(50, 300, 70, 30);
jframe.add(describe);
describe.setBounds(150, 300, 150, 30);
jframe.add(save);
save.setBounds(130, 350, 50, 50);
jframe.add(cancel);
cancel.setBounds(220, 350, 50, 50);
jframe.setLayout(null);
jframe.setVisible(true);
jframe.setSize(500, 500);
country.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
city.removeAllItems();
if(country.getSelectedItem().equals("India")){
for(String s : citiesIndia){
city.addItem(s);
}
}
if(country.getSelectedItem().equals("Pakistan")){
for(String s : citiesPakistan){
city.addItem(s);
}
}
if(country.getSelectedItem().equals("USA")){
for(String s : citiesUS){
city.addItem(s);
}
}
city.setEnabled(true);
}
});
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int input = JOptionPane.showConfirmDialog(null, "Details Saved");
if (input == 0) {
FileWriter fw = null;
File file = new File("survey.txt");
try {
if (file.exists()) {
fw = new FileWriter(file, true);
} else {
fw = new FileWriter(file);
}
fw.write("\nName : " + name.getText());
fw.write("\nCity : " + city.getSelectedItem());
fw.write("\nCountry : " + country.getSelectedItem());
fw.write("\nAddress : " + address.getText());
fw.write("\nHobbies : " + hobbies.getText());
fw.write("\nDescription : " + describe.getText());
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
fw.close();
clearAllOPtions();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearAllOPtions();
}
});
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class SwingSurvey {
public static void main(String[] args) {
Survey s = new Survey();
}
}
运行此命令并单击“保存”按钮时,出现如下所示的空指针异常:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at swings.Survey$1.actionPerformed(SwingSurvey.java:100)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at swings.Survey.clearAllOPtions(SwingSurvey.java:34)
at swings.Survey.access$0(SwingSurvey.java:32)
at swings.Survey$2.actionPerformed(SwingSurvey.java:144)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
.....
我遇到了类似上面的异常。该如何纠正?
答案 0 :(得分:2)
这可能是因为您设置了null
country.setSelectedItem( null );
然后致电
country.getSelectedItem().equals("India")
得到NPE。
要纠正这种情况,请使用字符串比较之类的方式,
if ( "India".equals( country.getSelectedItem() ))