2016-02-01 42 views
0

我想轉換strSqliteDate:= 2016-01-14 05 :34:50 PM2016年1月14日。之後將所有時間我的應用程序是的引起:java.lang.NullPointerException:嘗試在Android中的空對象引用上調用虛擬方法'long java.util.Date.getTime()'

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) 

崩潰和獲取錯誤在該行date = dateFormat.parse(strSqliteDate);

,並在此line = calendar.setTime(date);

第二收到錯誤

所致:顯示java.lang.NullPointerException:嘗試 ')長java.util.Date.getTime(' 上的空對象引用 在java.util.Calendar.setTime調用虛擬方法 (Calendar.java :1195)

以下是在DBhelper類日期轉換代碼

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); 
+0

http://stackoverflow.com/a/3930115/5456493 – Abhishek

回答

3

在使用變量strSqliteDate之前檢查它是否爲null?

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); 
} 
+0

它的工作,謝謝您的幫助 !! –

+0

@ p.ld繼續前進。 –

0

使用SUBSTRING方法是指Refers link

strSqliteDate.subString(0,10) 

Output will be==>> 2016-01-14 
0

date = dateFormat.parse(strSqliteDate); strSqliteDate就是爲什麼ü越來越NPE

1

產生的原因:顯示java.lang.NullPointerException:試圖調用虛擬 方法「長java.util.Date .getTime()'爲空對象引用 java.util.Calendar.setTime(Calendar.java:1195)

根據您的問題,您應該使用substring() &檢查您的數據庫是否爲null或不。

此方法有兩個變體,並返回一個新字符串,該字符串是此字符串的一個子字符串 。子字符串以 指定的索引字符開始,並擴展到該字符串的末尾,或者最大爲 endIndex - 如果給出第二個參數,則爲1。

.substring(startIndex, endIndex); 

strSqliteDate = 2016年1月14日下午5點34分五十秒

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

0

在行

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

通過日期類的引用的formate()的說法..

相關問題