0

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 ~

3
  • Timestamp is null. Not normal null, it's the literal string "null". We can't tell why because that code isn't given. Commented Sep 2, 2020 at 21:20
  • You need to post the lines where the timestamp data is being set: chatList.get(position).getTimestamp(). Probably just need to initialize the attribute in the get(position) object. Commented Sep 2, 2020 at 21:31
  • For example in the constructor of the object returned from this call chatList.get(position) initialize the timestamp directly :: this.timestamp = String.valueOf(Instant.now().getEpochSecond()); Or maybe initialize it with data from your db or some other way. Commented Sep 2, 2020 at 21:49

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.