0

I want to convert strSqliteDate﹕ = 2016-01-14 05:34:50 PM into 2016-01-14 .After converting all dates my application was crash and getting error of

02-01 12:03:15.204    7619-7619/com.example.tazeen.classnkk W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
02-01 12:03:15.204    7619-7619/com.example.tazeen.classnkk W/System.err﹕ at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1035)
02-01 12:03:15.204    7619-7619/com.example.tazeen.classnkk W/System.err﹕ at java.text.DateFormat.parse(DateFormat.java:577)

At this line date = dateFormat.parse(strSqliteDate);

and second getting error at this line = calendar.setTime(date);

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference at java.util.Calendar.setTime(Calendar.java:1195)

Here is date conversion code in DBhelper class

String strSqliteDate = cursor.getString(cursor.getColumnIndex("ActionDate"));
                Log.e(" strSqliteDate "," = " + strSqliteDate);

                String result = "";
                Date date = null;
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try
                {
                     date = dateFormat.parse(strSqliteDate);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                SimpleDateFormat printFormat = new SimpleDateFormat("yyyy-MM-dd");
                result = printFormat.format(calendar.getTime());
                Log.e("result = ","==========>"+result);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
p. ld
  • 585
  • 3
  • 10
  • 23

5 Answers5

5

Before Use variable strSqliteDate check whether it is null or not?

String strSqliteDate = cursor.getString(cursor.getColumnIndex("ActionDate"));
if(strSqliteDate != null && !strSqliteDate .isEmpty())
{

  Log.e(" strSqliteDate "," = " + strSqliteDate);

            String result = "";
            Date date = null;
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try
            {
                 date = dateFormat.parse(strSqliteDate);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            SimpleDateFormat printFormat = new SimpleDateFormat("yyyy-MM-dd");
            result = printFormat.format(calendar.getTime());
            Log.e("result = ","==========>"+result);
}
sasikumar
  • 12,540
  • 3
  • 28
  • 48
1

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference at java.util.Calendar.setTime(Calendar.java:1195)

According to your Question ,You should use substring() & Do check your database is null or not .

This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex - 1 if second argument is given.

.substring(startIndex, endIndex); 

strSqliteDate = 2016-01-14 05:34:50 PM

System.out.println(strSqliteDate.substring(0, 11) );

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

Use the subString method refers Refers link

strSqliteDate.subString(0,10)

Output will be==>> 2016-01-14 
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
0

Your date = dateFormat.parse(strSqliteDate); strSqliteDate is null that is why u are getting NPE

Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
JAAD
  • 12,349
  • 7
  • 36
  • 57
0

At line

result = printFormat.format(calendar.getTime());

pass the reference of Date class in the argument of formate()..

NEERAJ GUPTA
  • 125
  • 1
  • 11