[Solved] nullpointer exception in parsing string to date [closed]


First make sure that from2 is not null and data getting from db is not null.

Following added the success and failure cases,

String           from2            = "01-12-2015";
SimpleDateFormat simpleDateFromat = new SimpleDateFormat("dd-MM-yyyy");
Date dateFROM                     = simpleDateFromat.parse(from2);  
System.out.println("dateFROM : "+dateFROM);

Here you will get the correct date as : Tue Dec 01 00:00:00 IST 2015

from2 = "2015-12-01";
dateFROM = simpleDateFromat.parse(from2);
System.out.println("dateFROM : "+dateFROM);

Here because of date is not in correct format it parse it as : Tue Jun 07 00:00:00 IST 7

from2 = "";
dateFROM = simpleDateFromat.parse(from2);
System.out.println("dateFROM : "+dateFROM);

here you will get an java.text.ParseException: Unparseable date: ""

from2 = null;
dateFROM = simpleDateFromat.parse(from2);
System.out.println("dateFROM : "+dateFROM);

here you will get an java.lang.NullPointerException

solved nullpointer exception in parsing string to date [closed]