运行以下示例时,我收到NullPointerException:
private void contractExpiryActionPerformed(java.awt.event.ActionEvent evt) {
String expireDate = contractExpiry.getSelectedItem().toString();
System.out.println(expireDate);
}
只想打印出日期以测试它是否正常工作,但无论我做什么,我都会不断收到此错误。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at tetrapro.IncomeSolution.contractExpiryActionPerformed(IncomeSolution.java:865) at tetrapro.IncomeSolution.lambda$initComponents$6(IncomeSolution.java:492) at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258) at javax.swing.JComboBox.contentsChanged(JComboBox.java:1332) at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1352) at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179) at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:174) at javax.swing.JComboBox.removeAllItems(JComboBox.java:771) at tetrapro.IncomeSolution.setExpirationDates(IncomeSolution.java:250) at tetrapro.IncomeSolution.(IncomeSolution.java:61) at tetrapro.IncomeSolution.lambda$main$11(IncomeSolution.java:1007) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:726) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:1)
很可能contractExpiry为null getSelectedItem()返回null。 使用IDE的调试选项,并在示例代码的第3行显示断点
OR
private void contractExpiryActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("contractExpiry = " + contractExpiry);
System.out.println("contractExpiry.getSelectedItem() = " + contractExpiry.getSelectedItem());
String expireDate = contractExpiry.getSelectedItem().toString();
System.out.println(expireDate);
}
这实际上显示了NullPointerException发生的位置:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tetrapro.IncomeSolution.contractExpiryActionPerformed(IncomeSolution.java:865)
答案 1 :(得分:1)
private void displayOptions(String symbol){
int count = contractExpiry.getItemCount();
if(count != 0){
String expire = contractExpiry.getSelectedItem().toString();
x = expire.substring(expire.length()-5, expire.length());
days = data.getDaysToExpire(symbol, x);
displayNumDays.setText(days);
data.getSelectedData_F(symbol, x);
}
String last = data.getFuturePrice(contractBox.getSelectedIndex());
displayPrice.setText(last);
我必须测试jcombobox是否为空。当它是空的时它给了我错误。现在,如果项目计数为0,则将跳过。因此,没有更多的错误。谢谢大家的帮助。