Get java.lang.NumberFormatException: For input string: "null" when trying to show data in message

huangapple go评论74阅读模式
英文:

Get java.lang.NumberFormatException: For input string: "null" when trying to show data in message

问题

我一直在遇到这个问题,我试图在一个消息应用中显示消息发布的日期,但我一直在收到这个错误,应用程序一直崩溃。

String message = chatList.get(position).getMessage();
String timeStamp = chatList.get(position).getTimestamp();

Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(Long.parseLong(timeStamp));
String dateTime = DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();

holder.messageTv.setText(message);
holder.timeTv.setText(dateTime);

这是我收到的日志错误:

2020-09-02 22:10:08.411 13445-13445/com.x00122898.meetupproject E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.x00122898.meetupproject, PID: 13445
    java.lang.NumberFormatException: For input string: "null"
        at java.lang.Long.parseLong(Long.java:594)
        at java.lang.Long.parseLong(Long.java:636)
        at com.x00122898.meetupproject.ChatActivity$1.onDataChange(ChatActivity.java:107)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
2020-09-02 22:10:08.422 13445-13445/com.x00122898.meetupproject I/Process: Sending signal. PID: 13445 SIG: 9

任何帮助将不胜感激~

英文:

I keep coming across this issue, I'm trying to show the date the message has been posted in a messaging application and I keep getting this error and the application keeps crashing.

String message = chatList.get(position).getMessage();
        String timeStamp = chatList.get(position).getTimestamp();

        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(Long.parseLong(timeStamp));
        String dateTime = DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();

        holder.messageTv.setText(message);
        holder.timeTv.setText(dateTime);

This is the logcat error I receive:

2020-09-02 22:10:08.411 13445-13445/com.x00122898.meetupproject E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.x00122898.meetupproject, PID: 13445
    java.lang.NumberFormatException: For input string: "null"
        at java.lang.Long.parseLong(Long.java:594)
        at java.lang.Long.parseLong(Long.java:636)
        at com.x00122898.meetupproject.ChatActivity$1.onDataChange(ChatActivity.java:107)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
2020-09-02 22:10:08.422 13445-13445/com.x00122898.meetupproject I/Process: Sending signal. PID: 13445 SIG: 9

Any help would be greatly appreciated ~

答案1

得分: 0

问题出在以下两行代码:

String timeStamp = chatList.get(position).getTimestamp();
cal.setTimeInMillis(Long.parseLong(timeStamp));

你的变量timeStamp包含字符串/单词"null",当你试图将其解析为Long时,会抛出NumberFormatException异常。
你必须捕获这个异常或者验证timeStamp是否包含数值(参见如何在Java中检查字符串是否为数字)。

另外,也许getTimestamp()方法没有返回正确的变量,或者时间戳从未被保存或发送过。

英文:

The problem is in these two lines of code:

String timeStamp = chatList.get(position).getTimestamp();
cal.setTimeInMillis(Long.parseLong(timeStamp));

Your variable timeStamp contains the string/word "null", and when you try to parse it as Long, it throws the NumberFormatException.
You must catch the exception or verify if timeStamp contains a numeric value (see How to check if a String is numeric in Java).

Also, maybe the method getTimestamp() is not returning the right variable or the timestamp was never saved or send

答案2

得分: 0

看起来问题出在ChatActivity.java的第107行,其中的值字面上是"null"。请查看先前的答案numberformatexception-for-input-string-null

英文:

It looks like the problem occurs at ChatActivity.java line 107 where the value is literally "null". See previous answers
numberformatexception-for-input-string-null

huangapple
  • 本文由 发表于 2020年9月3日 05:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63713662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定