在此我試圖從mysql數據庫中獲取日期,並通過設置color.But在JPanel jPanel = jDayChooser1.getDayPanel()
中顯示這些日期,即時獲取錯誤'NullPointerException'。在JPanel中獲取NullPointerException jPanel = jDayChooser1.getDayPanel()
public Calender() {
// retrieving the dates from db
getDates c = new getDates();
Date[] a = c.Dates();
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
JPanel jPanel = jDayChooser1.getDayPanel();
Component component[] = jPanel.getComponents();
for(int i=0;i<a.length;i++) {
if(month == a[i].getMonth() && year == a[i].getYear()) {
component[ a[i].getDay()].setBackground(Color.blue);
}
}
initComponents();
}
這裏的堆棧跟蹤...
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at greetingcard.Calender.<init>(Calender.java:37) at greetingcard.Calender$1.run(Calender.java:100) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:682) at java.awt.EventQueue$3.run(EventQueue.java:680) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:691) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) BUILD SUCCESSFUL (total time: 5 seconds)
添加您的堆棧跟蹤。 – Zyn
我添加了堆棧跟蹤:) – user2700115
你在哪裏創建'jDayChooser1'? – AKS