由以下原因引起:java.lang.NullPointerException:尝试在 Android 中的空对象引用上调用虚方法“long java.util.Date.getTime()”

新手上路,请多包涵

我想将 strSqliteDate﹕ = 2016-01-14 05:34:50 PM 转换为 2016-01-14 。转换所有日期后,我的应用程序崩溃并出现错误

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.Calendar.setTime(Calendar.java:1195) 的空对象引用上调用虚方法“long java.util.Date.getTime()”

这是 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);

原文由 p. ld 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 384
2 个回答

在使用变量 strSqliteDate 之前检查它是否为空?

 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 发布,翻译遵循 CC BY-SA 3.0 许可协议

原因:java.lang.NullPointerException:尝试在 java.util.Calendar.setTime(Calendar.java:1195) 的空对象引用上调用虚方法“long java.util.Date.getTime()”

根据您的问题,您应该使用 substring() 并检查您的 数据库 是否为 null

此方法有两个变体并返回一个新字符串,该字符串是该字符串的子字符串。子字符串以指定索引处的字符开始,并延伸到该字符串的末尾,或者如果给出第二个参数,则延伸到 endIndex - 1。

 .substring(startIndex, endIndex);

strSqliteDate = 2016-01-14 下午 5:34:50

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

原文由 IntelliJ Amiya 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题