[英]Don't understand this NullPointerException with ComboBox
I can't understand why I'm getting this NullPointerException. 我不明白为什么会收到此NullPointerException。 I started with much different looking code (and improved to what you see) all in attempts of fixing the bug and I just can't figure out what's going on.
我从外观迥异的代码开始(并改进为您看到的内容),试图修复该错误,但我只是无法弄清发生了什么。
all help is hugely appreciated: 非常感谢所有帮助:
final JComboBox modelName = null;
//Model ComboBoxModel
final DefaultComboBoxModel termModel = new DefaultComboBoxModel(new String[]{
"Choose One...", "Hypercom", "DejaVoo", "Nurit", "Verifone"});
...
// model drop-down
modelName.setModel(termModel); //getting nullPointerException on this line
modelName.addActionListener(this);
modelPanel.add(modelName);
...
@Override
public void actionPerformed(ActionEvent e) {
if ("Hypercom".equals(modelName.getSelectedItem())){
termName.setModel(hSpecModel);
} else if ("Deja Voo".equals(modelName.getSelectedItem())){
termName.setModel(dSpecModel);
} else if ("Nurit".equals(modelName.getSelectedItem())){
termName.setModel(nSpecModel);
} else if ("Verifone".equals(modelName.getSelectedItem())){
termName.setModel(vSpecModel);
} else {
termName.setModel(slctAbove);
}
}
modelName
has not been initialized, so it doesn't point to an object. modelName
尚未初始化,因此未指向对象。
You need something like 你需要类似的东西
final JComboBox modelName = new JComboBox();
final JComboBox modelName = null; // !!!!!
当您尝试使用此变量时,您真的不应该奇怪为什么会得到NPE:
modelName.setModel(termModel);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.