-7

I am getting date from the calender and converting its format using

    SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy");
    Date date = null;
    try {
        date = sdf1.parse(datevalue);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    if(date != null) {
        Date d = new Date(date.getTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        return sdf.format(d);
    }

    return "";

But I am getting crash on some of devices and for some devices it is working fine.
I don't know why it is happening on some devices only.

I am getting this in Fabric report:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference    
       at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1044)
       at java.text.DateFormat.parse(DateFormat.java:577)
       at com.app.appUtilities.Utilities.convertDateFormat(Utilities.java:34)

Please help me out.

paras kochar
  • 29
  • 1
  • 1
  • 7
  • 6
    `datevalue` is obviously `null` in certain circumstances. – Bathsheba Oct 03 '16 at 07:22
  • You assigned null to date reference on the second line. – optimistic_creeper Oct 03 '16 at 07:24
  • it is initialized in third line as you can see if u have any fair answer then comment – paras kochar Oct 03 '16 at 07:27
  • @paraskochar as Bathsheba said, `dateValue` is null in some circumstances. It's a simple NPE and all you've got to do is make sure it isn't null! – Sufian Oct 03 '16 at 07:38
  • @Sufain I am getting date in form of mm/dd/yyyy from user and converting this in yyyy-MM-dd'T'HH:mm:ss.SSSZ form how it can be null public static String convertDateFormat(String datevalue) { SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy"); Date date = null; try { date = sdf1.parse(datevalue); } catch (ParseException e) { e.printStackTrace(); } if(date != null) { Date d = new Date(date.getTime()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); return sdf.format(d); } return ""; } – paras kochar Oct 03 '16 at 07:43
  • @paraskochar I said, `datevalue` is null, not `data`! – Sufian Oct 03 '16 at 08:41
  • @paraskochar `datevalue` is a `String`. Check your code. If you're really interested, just check the line 1044 of `SimpleDateFormat.parse()`. – Sufian Oct 03 '16 at 08:43
  • datevalue is the string variable having date that I am getting from calender in mm/dd/yyyy format , and this code is working absolutely fine in many android devices – paras kochar Oct 03 '16 at 08:57
  • @paraskochar then update your question with the code where you're setting values to `datevalue`. All places where you're assigning something to it. – Sufian Oct 03 '16 at 10:09

1 Answers1

1
    String dateStr = "04/05/2010"; 

    SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); 

    try {
        dateObj = curFormater.parse(dateStr);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy"); 

    String newDateStr = postFormater.format(dateObj); 
    Toast.makeText(getApplicationContext(), newDateStr, 1).show();
LaurentY
  • 7,495
  • 3
  • 37
  • 55
  • SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy"); Date date = null; try { date = sdf1.parse(datevalue); } catch (ParseException e) { e.printStackTrace(); } if(date != null) { Date d = new Date(date.getTime()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); return sdf.format(d); } return ""; – paras kochar Oct 03 '16 at 07:49