问题
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