简体   繁体   中英

Convert jcalendar Date into XMLGregorianCalendar Getting Null Value

i have some problem, i just getting null value from jDateChooser in jCalendar.

This method is function to convert java.util.Date into 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);
    }
}

And this is 2 function which getStart and getEnd Dates.

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);
}

Then i just place the method inside an object called schedule:

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

Result from 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)

what's wrong?

Thanks before.

UPDATE ::

I just change the function into this:

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);
    }
}

UPDATE 2# ::

After initializing the newInstance() method, i'm getting another error:

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

I just change the package name from

java.util.Date into java.SQL.Date

then casting:

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

How to resolve this issue?

thanks again.

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?

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(); 

我并不完全理解你的问题,但在我看来,这是一个简单的修复调用你的日历,这应该工作(我认为)。

Calendar timeStamp = new GregorianCalendar();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM