您好,我在运行程序时遇到空指针错误。我在这个网站上找到了一篇我认为可以解决问题的帖子 Null Pointer Exception from JCalander Combobox我使用了此页面中的建议,但仍然出现错误。谁能告诉我哪里出错了?
String end;
if (jTimeButton3 != null) {
SimpleDateFormat dateFormatTime2 = new SimpleDateFormat("hh:mm a");
end = dateFormatTime2.format(jTimeButton3.getTargetDate());
endTime.setText(end);
} else {
JOptionPane.showMessageDialog(
null, "Please select a End Time.");
return;
}
最佳答案
你得到一个 NullPointerException 因为你的 jTimeButton3.getTargetDate()
是 Null:
您可以通过测试您的日期来修复它:
String end;
if (jTimeButton3 != null && jTimeButton3.getTargetDate() != null) {
SimpleDateFormat dateFormatTime2 = new SimpleDateFormat("hh:mm a");
end = dateFormatTime2.format(jTimeButton3.getTargetDate());
endTime.setText(end);
} else {
JOptionPane.showMessageDialog(
null, "Please select a End Time.");
return;
}
关于java - 我在 Java 程序中遇到 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17005882/