0

在这个我试图从mysql数据库中获取日期并通过设置颜色在jDayChooser中显示这些日期。但JPanel jPanel = jDayChooser1.getDayPanel()我得到错误'NullPointerException'。

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)

4

1 回答 1

0

明白了.. :)

 public Calender() {
 initComponents();

// 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();
}
于 2014-03-31T19:42:29.640 回答