Home > database >  Text 'null' could not be parsed at index 0 error
Text 'null' could not be parsed at index 0 error

Time:02-20

i used threeten and Google Play console says my code only occurs in Samsung s9 and s20. here is my error and my code

Error Message

org.threeten.bp.format.DateTimeParseException: Text 'null' could not be parsed at index 0

FATAL EXCEPTION: Thread-3
Process: com.playhead.timbre, PID: 20573
org.threeten.bp.format.DateTimeParseException: Text 'null' could not be parsed at index 0
    at org.threeten.bp.format.DateTimeFormatter.parseToBuilder(DateTimeFormatter.java:1588)
    at org.threeten.bp.format.DateTimeFormatter.parse(DateTimeFormatter.java:1491)
    at org.threeten.bp.LocalDateTime.parse(LocalDateTime.java:444)
    at com.playhead.timbre.Function.localTime(Function.java:581)
    at com.playhead.timbre.Feed.Feed_Main.TimeOutRefresh(Feed_Main.java:229)
    at com.playhead.timbre.Feed.Feed_Frame.onHiddenChanged(Feed_Frame.java:91)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:374)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
    at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)

My Code [Function.Class]

    public static boolean localTime(String timeout){ //timeout = "2022-02-20 18:20:36"

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime serverDate = LocalDateTime.parse(timeout, formatter);  //<< Function.java:581
            org.threeten.bp.LocalDateTime localDate = org.threeten.bp.LocalDateTime.now();

            Log.e("LocalTime", "server:"   serverDate   " local:"   localDate);

            return localDate.isAfter(serverDate);

    }

CodePudding user response:

You get the exception org.threeten.bp.format.DateTimeParseException: Text 'null' could not be parsed at index 0 when you pass a string of length 4 containing "null" into the LocalDateTime.parse method.

If you would be passing an actual value of null, the exception would have been java.lang.NullPointerException: text must not be null.

I checked both cases in actual code by calling org.threeten.bp.format.LocalDateTime.parse(text, formatter).

At the call site(s) you should make a try/catch block to handle all invalid timestamps, or you could do checks to avoid passing invalid timestamps to the parse method.

If you decide to go with the try/catch approach, your code at call site(s) should be updated to something like this

try {
  String timeout = ... // your current code getting the timeout
  boolean isAfterServerDate = localTime(timeout)
  // ... other code
}
catch(Exception e) {
  // parsing of the timeout failed for some reason
  // e.getMessage contains the reason
}
// ... other code

If you decide to go without try/catch then you have to check for all invalid timestamp cases. Text containing "null" is one such case, but maybe you will have an empty string ("") sometimes as well? What about an actual null value, can that happen in your code? I suggest the try/catch approach.

  • Related