*
The moose likes Java in General and the fly likes using calendar as a date type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "using calendar as a date type" Watch "using calendar as a date type" New topic
Author

using calendar as a date type

Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
So eclipse tells me that date type is depricated. So i started using Calendar
consider a class as



I get the error
Exception in thread "main" java.lang.NullPointerException
at myclass.setmydate(Test.java:12)
at Test.main(Test.java:26)

Any idea why the date object is not being set ???
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rajesh Khan wrote:So eclipse tells me that date type is depricated.


No it didn't, because Date is not deprecated.

So i started using Calendar


Date and Calendar serve different roles, and are often used together. A Date represents an instant in time, without regard to human notations like years, months, time zones, etc. A Calendar represents a particular human convention for naming that instant in time.


I get the error
Exception in thread "main" java.lang.NullPointerException
at myclass.setmydate(Test.java:12)
at Test.main(Test.java:26)

Any idea why the date object is not being set ???


Because you're not setting it. The error messages don't lie. Since I can't see your entire code and don't know which line it's talking about, my guess it that you're making the very common error of having a local variable that hides a member:



In this class, the setX method does not set our x member variable.

Are you sure it's the date variable (Note: variable, not object) which is not being set? I can't see the line it's complaining about, but perhaps you're misinterpreting the error message and it's a different variable that's not set.
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
import java.util.Calendar;

Here is the code
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

And why is it that you think mydate will not be null?

(Also, it will be easier for others to read your code, and thus you'll get better help, if you follow Java's naming conventions. Class names start with uppercase and use camel case. Methods and variables start with lowercase and use camel case:


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rajesh Khan wrote:
Error is
Exception in thread "main" java.lang.NullPointerException
at myclass.setmydate(Test.java:12)
at Test.main(Test.java:23)


This doesn't help us, since we don't know which line is #23 in your code. You have to clearly indicate which line it is, or else paste in the exact code that you compiled so that the forum's line numbers will match your own.

Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Calendar is not a primitive type need to create an instance of it first.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Although I agree your information has come in bits and spurts and is still incomplete, I think your problem is liable to be "private Calendar mydate;" because you do not show any code that initializes mydate; then setmydate attempts to dereference it, producing a null pointer. Somewhere some code has to do "mydate = new Calendar();" or its equivalent.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ralph Cook wrote:Although I agree your information has come in bits and spurts and is still incomplete, I think your problem is liable to be "private Calendar mydate;"


Yup. That's why I asked why he thought it wouldn't be null--hoping to get him examining his code and thinking about what it's doing.
 
jQuery in Action, 2nd edition
 
subject: using calendar as a date type
 
Similar Threads
What fields does java.util.Date.equals compare?
How to compare (Hour) with actual new Date() ?
contains method in Vector Class
Object Control Creation – From Constructor?
Using Deprecated API in Code