Convert jcalendar Date into XMLGregorianCalendar Getting Null Value
Asked Answered
F

3

0

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.

Farther answered 26/8, 2012 at 13:54 Comment(5)
Youre still not instantiating df, see my update below.Funicular
yes, i need to instantiating the df var. thanks for your commentFarther
hi there @Funicular could you help me again? thanksFarther
Sure, but perhaps in a new question posting?Funicular
hi @Funicular i just posted in new question post: #12131568Farther
F
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(); 
Funicular answered 26/8, 2012 at 13:59 Comment(1)
aha, thanks Reimeus. that's the answer. i need to create newInstance() first.Farther
S
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?

Slipcover answered 26/8, 2012 at 13:58 Comment(1)
yes, i just forgot to place the initialization method. @Slipcover could you help me again? (update 2#) please. thanksFarther
F
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(); 
Funicular answered 26/8, 2012 at 13:59 Comment(1)
aha, thanks Reimeus. that's the answer. i need to create newInstance() first.Farther
E
0

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();
Ebneter answered 26/8, 2012 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.