AK824
AK824

Reputation: 217

Java Date Class NullPointerException

I am trying to set and return the earliest date from a string and I think I am missing something when setting my date as I keep getting a nullreferenceexception whenever I try to set the values for Date. Thanks for any help

private static Date createDate(String input)
{
    Date date = null;

    if (input == null)
        return null;

    // Split formatted input into separate values
    String tempDates[] = input.split(dateSep);

    // Store values as integers
    int[] dateValues = {0, 0, 0};
    dateValues[0] = Integer.parseInt(tempDates[0]);
    dateValues[1] = Integer.parseInt(tempDates[1]);
    dateValues[2] = Integer.parseInt(tempDates[2]);

    // Sort integers from lowest to highest
    Arrays.sort(dateValues);

    // Set return date
    date.setMonth(dateValues[0]);
    date.setDate(dateValues[1]);
    date.setYear(dateValues[2]);


    System.out.println(date);
    // Checking basic date restrictions
    if (date.getMonth() <= 0 || date.getMonth() > 12)
        throw new IllegalArgumentException("Month is not valid " + month);

    if (date.getDay() <= 0 || date.getDay() > 31)
        throw new IllegalArgumentException("Day is not valid " + day);

    if (date.getYear() <= 0)
        throw new IllegalArgumentException("Year is not valid " + year);


    return date;
}

}

Upvotes: 0

Views: 9372

Answers (4)

Memillopeloz
Memillopeloz

Reputation: 58

As all the other answers state, you need to initialize date, otherwise you will always get a null pointer/reference exception:

     date = new Date()

Furthermore, sorting the array does not make ensure a Month/Day/Year format as expected with your code:

    date.setMonth(dateValues[0]);
    date.setDate(dateValues[1]);
    date.setYear(dateValues[2]);

Even though I understand you want to retrieve the earliest date from the string, it is better if you validate these restrictions directly with tempDate

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122008

You wrote Date date = null;

you initialized with null.

and doing operation on null.

what you have to do is

date = ..evaluate value here....

or as others mentioned,Assign new Date() to it and do something.

 date.setMonth(dateValues[0]);

Upvotes: 1

Prabhaker A
Prabhaker A

Reputation: 8473

You need to initialize Date object.
Change this line to Date date = null; to Date date = new Date();.

Normally you will get NullPointerException

When you attempts to use null in a case where an object is required. These include:
1.Calling the instance method of a null object.
2.Accessing or modifying the field of a null object.
3.Taking the length of null as if it were an array.
4.Accessing or modifying the slots of null as if it were an array.
5.Throwing null as if it were a Throwable value.

Upvotes: 2

Luke Taylor
Luke Taylor

Reputation: 9599

You have not initialized the variable date. To have a look at initializing it, have a look at the following post: http://www.tutorialspoint.com/java/java_date_time.htm

date = new Date();

or

//The following constructor accepts one argument that equals the number of milliseconds that // have elapsed since midnight, January 1, 1970
date = new Date(long millisec);

I hope this helps.

Upvotes: 0

Related Questions