简体   繁体   English

java.lang.NumberFormatException?

[英]java.lang.NumberFormatException?

I'm new to android programming.我是 android 编程的新手。 I have problem in Chat App.我在聊天应用程序中遇到问题。 When is send message to any user, user message stores in Firebase Database.当向任何用户发送消息时,用户消息存储在 Firebase 数据库中。 But when i try to show all the chats of specific user, it gives me an error.但是当我尝试显示特定用户的所有聊天时,它给了我一个错误。

The problem is java.lang.NumberFormatException: null问题是 java.lang.NumberFormatException: null

Here's my Code where im getting error:这是我遇到错误的代码:

@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {

    //get data
    String message = chatList.get(i).getMessage();
    String timestamp = chatList.get(i).getTimestamp();

    //convert time stamp to dd/mm/yyyy hr:mm am/pm
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(Long.parseLong(timestamp));
    //cal.setTimeInMillis(Long.parseLong(timestamp));
    String dateTime = android.text.format.DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();

    //set Data
    myHolder.messageTv.setText(message);
    myHolder.timeTv.setText(dateTime);

    //msg delivered/seen
    if (i == chatList.size()-1){
        if (chatList.get(i).isSeen()){
            myHolder.isSeenTv.setText("Seen");
        }
        else {
            myHolder.isSeenTv.setText("Delivered");
        }
    }
    else {
        myHolder.isSeenTv.setVisibility(View.GONE);
    }

}

enter image description here在此处输入图像描述

Can you post full exception stacktrace?您可以发布完整的异常堆栈跟踪吗? It's possible that this line causes it assuming your timestamp is null假设您的时间戳是 null

Long.parseLong(timestamp)

Use try-catch block to handle NumberFormatException使用try-catch块处理NumberFormatException

Calendar cal = Calendar.getInstance(Locale.ENGLISH);

try {
    cal.setTimeInMillis(Long.parseLong(timestamp)); 
} catch(Exception ex) {
    ex.printStackTrace();
}

String dateTime = android.text.format.DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM