Convert jcalendar Date into XMLGregorianCalendar Getting Null Value

别来无恙 提交于 2019-12-12 05:20:47

问题


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.


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



回答2:


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?




回答3:


I'm not exactly understanding your problem but seems to me this is an easy fix call your Calendar like this should work (i think).

Calendar timeStamp = new GregorianCalendar();


来源:https://stackoverflow.com/questions/12130680/convert-jcalendar-date-into-xmlgregoriancalendar-getting-null-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!