0

我有一些问题,我只是从 jCalendar 中的 jDateChooser 获取空值。

此方法是将 java.util.Date 转换为 XMlGregorianCalendar 的函数:

DatatypeFactory df;
public XMLGregorianCalendar function_ConvertAsXMLGregorianCalendar(Date date) {
    if (date == null) {
        System.out.println("Error on Function Convert Date into XML Gregorian Calendar");
        return null; 
    } else {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(date.getTime());
        return df.newXMLGregorianCalendar(gc);
    }
}

这是 getStart 和 getEnd Dates 的 2 个函数。

private XMLGregorianCalendar getStartDate(){
    Date dateStarting  = jDateChooserStart.getDate();
    System.out.println("Date Start : " + dateStarting.toString());
    XMLGregorianCalendar cal = function_ConvertAsXMLGregorianCalendar(dateStarting);
    System.out.println("Converted Date : " + cal.toXMLFormat());
    return cal;
}

private XMLGregorianCalendar getEndDate(){
    Date dateEnding = jDateChooserEnd.getDate();
    System.out.println("Date End : " + dateEnding);
    return function_ConvertAsXMLGregorianCalendar(dateEnding);
}

然后我只是将方法放在一个名为 schedule 的对象中:

schedule.setStartDate(getStartDate());
schedule.setEndDate(getEndDate());

来自 Netbeans(v7.1) 的结果

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Frames.CreateSchedule.function_ConvertAsXMLGregorianCalendar(CreateSchedule.java:181)
at Frames.CreateSchedule.getStartDate(CreateSchedule.java:188)
at Frames.CreateSchedule.SubmitButtonActionPerformed(CreateSchedule.java:204)
at Frames.CreateSchedule.access$000(CreateSchedule.java:16)

怎么了?

之前谢谢。

更新 ::

我只是将功能更改为:

public XMLGregorianCalendar function_ConvertAsXMLGregorianCalendar(Date date) {
    if (date == null) {
        System.out.println("Error on Function Convert Date into XML Gregorian Calendar");
        return null; 
    } else {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(date.getTime());
        DatatypeFactory df = null;
        return df.newXMLGregorianCalendar(gc);
    }
}

更新 2#::

初始化 newInstance() 方法后,我收到另一个错误:

java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

我只是从

java.util.Date into java.SQL.Date

然后铸造:

Date dateStarting  = (Date) jDateChooserStart.getDate();
Date dateEnding    = (Date) jDateChooserEnd.getDate();

如何解决这个问题?

再次感谢。

4

3 回答 3

1

Though not obvious without proper line numbers in your code, the most likely cause of the NullPointerException is the line:

  return df.newXMLGregorianCalendar(gc);

with your df being null. Where do you initialize this field?

于 2012-08-26T13:58:43.743 回答
1

It appears that df is declared but not instantiated:

DatatypeFactory df;

from here:

df.newXMLGregorianCalendar(gc);
^

You can use DatatypeFactory.newInstance() to instantiate first, like so:

DatatypeFactory df = DatatypeFactory.newInstance(); 
于 2012-08-26T13:59:39.770 回答
0

我不完全理解您的问题,但在我看来,这是一个简单的解决方法,调用您的日历,这样应该可以工作(我认为)。

Calendar timeStamp = new GregorianCalendar();
于 2012-08-26T18:42:18.280 回答