This question already has an answer here:
这个问题在这里已有答案:
- What is a NullPointerException, and how do I fix it? 12 answers
- 什么是NullPointerException,我该如何解决? 12个答案
I have tried to develop a simple swing application with actionlisteners. I used JComboBox. It has an actionlistener. When I perform some action on it and click save button I am getting an exception. When I checked with the value obtained in combo box, the value was correct.But still I am getting null pointer exception.
我试图用actionlisteners开发一个简单的swing应用程序。我用过JComboBox。它有一个actionlistener。当我对它执行某些操作并单击“保存”按钮时,我会收到异常。当我用组合框中获得的值检查时,值是正确的。但是我仍然得到空指针异常。
Code:
码:
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();
}
}
When I run this and click on save button, I am getting null pointer exception as below:
当我运行它并单击保存按钮时,我得到如下的空指针异常:
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)
.....
.....
I am getting the exception like above. How to rectify it?
我正如上面那样得到例外。如何纠正呢?
1 个解决方案
#1
2
This is probably because you set null,
这可能是因为你设置了null,
country.setSelectedItem( null );
and then call
然后打电话
country.getSelectedItem().equals("India")
This gives NPE.
这给了NPE。
To rectify this use string comparison like,
要纠正这个使用字符串比较,如,
if ( "India".equals( country.getSelectedItem() ))
#1
2
This is probably because you set null,
这可能是因为你设置了null,
country.setSelectedItem( null );
and then call
然后打电话
country.getSelectedItem().equals("India")
This gives NPE.
这给了NPE。
To rectify this use string comparison like,
要纠正这个使用字符串比较,如,
if ( "India".equals( country.getSelectedItem() ))