java - 摆动应用程序中的线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常
问题描述
我试图用动作监听器开发一个简单的摇摆应用程序。我使用了 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)
......
我得到了像上面这样的异常。如何纠正它?
解决方案
这可能是因为您设置了null,
country.setSelectedItem( null );
然后打电话
country.getSelectedItem().equals("India")
这给出了 NPE。
要纠正这种情况,请使用字符串比较,例如,
if ( "India".equals( country.getSelectedItem() ))
推荐阅读
- python - 实例化类并通过与类声明相同的模块中的函数调用其函数?
- sql - 错误:由于并发更新,无法序列化访问
- php - 如何在laravel中获取数组的某些元素?
- azure - 错误代码:从 Azure 流分析作业中采样数据时出现 BadArgument 错误消息
- database - Adonis.js give an error when try to run adonis serve --dev command
- angular - 如何使用 pipe() 从nestjs 获取错误到角度?
- python - Pandas:如何使用 Numpy 函数而不是 Lambda 函数来获得相同的结果(因为 Numpy 更快)?
- c# - Visual Studio 2012 与 ASP.NET MVC 4 与 Windows 7 中使用哪个实体框架?
- ios - 通用 UITableView 和 NSFetchedResultsController,继承自 UITableViewDelegate、UITableViewDataSource 和 NSFetchedResultsController
- java - 通过 Java 创建 HTTP GET 请求时遇到问题