20

I need your help in initializing a java.util.Date variable to empty. As I am running the page and it is showing NullPointerException if I didn't select any date.

The code is:

private java.util.Date date2;

I tried to make that variable empty, but it didn't work

private java.util.Date date2;

if (date2 == null || date2.equals(""))
    date2 = "";

However, with the initialization:

private java.util.Date date2 = new java.util.Date(0,0,0);

The above code will give me a default value which I don't want it.

djm.im
  • 3,295
  • 4
  • 30
  • 45
99maas
  • 1,239
  • 12
  • 34
  • 59
  • 1
    It's a bad idea to make thing like this (i.e. making nullPointerExceptions). – VadymVL May 11 '15 at 13:11
  • 3
    `date2 = ""` does not work, because `date2` is a `Date` and not a `String`. You cannot assign a `String` to a variable that is not a `String`. Also, `date2.equals("")` is always `false`. – Jesper May 11 '15 at 13:13
  • 2
    what do you mean by *initializing a java.util.Date variable to empty*? – Blip May 11 '15 at 13:15
  • @99maas You need some more basic learning about Java & OOP, specifically variables & object references. I suggest working the free-of-cost online [Java Tutorial](http://docs.oracle.com/javase/tutorial/) by Oracle. And read [Head First Java](http://www.amazon.com/Head-First-Java-2nd-Edition/dp/0596009208) by Kathy Sierra et al.; while outdated the basics have stayed the same. – Basil Bourque May 11 '15 at 16:00

5 Answers5

17

Instance of java.util.Date stores a date. So how can you store nothing in it or have it empty? It can only store references to instances of java.util.Date. If you make it null means that it is not referring any instance of java.util.Date.

You have tried date2=""; what you mean to do by this statement you want to reference the instance of String to a variable that is suppose to store java.util.Date. This is not possible as Java is Strongly Typed Language.

Edit

After seeing the comment posted to the answer of LastFreeNickname

I am having a form that the date textbox should be by default blank in the textbox, however while submitting the data if the user didn't enter anything, it should accept it

I would suggest you could check if the textbox is empty. And if it is empty, then you could store default date in your variable or current date or may be assign it null as shown below:

if(textBox.getText() == null || textBox.getText().equals(""){
    date2 = null; // For Null;
    // date2 = new Date(); For Current Date
    // date2 = new Date(0); For Default Date
}

Also I can assume since you are asking user to enter a date in a TextBox, you are using a DateFormat to parse the text that is entered in the TextBox. If this is the case you could simply call the dateFormat.parse() which throws a ParseException if the format in which the date was written is incorrect or is empty string. Here in the catch block you could put the above statements as show below:

try{
    date2 = dateFormat.parse(textBox.getText());
}catch(ParseException e){
    date2 = null; // For Null;
    // date2 = new Date(); For Current Date
    // date2 = new Date(0); For Default Date
}
Blip
  • 3,061
  • 5
  • 22
  • 50
11

IMO, you cannot create an empty Date(java.util). You can create a Date object with null value and can put a null check.

 Date date = new Date(); // Today's date and current time
 Date date2 = new Date(0); // Default date and time
 Date date3 = null; //Date object with null as value.
 if(null != date3) {
    // do your work.
 }
akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
1

Try initializing with null value.

private java.util.Date date2 = null;

Also private java.util.Date date2 = ""; will not work as "" is a string.

Kaushik
  • 57
  • 5
0

It's not clear how you want your Date logic to behave? Usually a good way to deal with default behaviour is the Null Object pattern.

LastFreeNickname
  • 1,445
  • 10
  • 17
  • I am having a form that the date textbox should be by default blank in the textbox, however while submitting the data if the user didn't enter anything, it should accept it – 99maas May 11 '15 at 19:51
0

An instance of Date always represents a date and cannot be empty. You can use a null value of date2 to represent "there is no date". You will have to check for null whenever you use date2 to avoid a NullPointerException, like when rendering your page:

if (date2 != null)
  // print date2
else
  // print nothing, or a default value