0

I'm very new to Java and regex and has no experience with android development (but I've been tasked to fix a bug). My question breaks down into two parts.

I have an error that shows:

Fatal Exception: java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.
...
...
...

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.matches(java.lang.String)' on a null object reference
       at utils.DataParsingUtils.isInteger + 195(DataParsingUtils.java:195)
       at services.FCMService.createChatNotification + 255(FCMService.java:255)

Doing some tracing, I believe the error originates from the file /utils/DataParsingUtils and from the function isInteger, which is shown below.

public static boolean isInteger(String s) {
    return s.matches("^-?\\d+$");
}

Based on the error, I think it has to do with a null value being returned. In this case from the isInteger function. This leads me to 2 questions.

  1. What exactly is the regex doing? My research tells me:

a) " = matches the beginning of the line b) - = indicates a range c) ? = does not occur or occurs once d) \\d = matches the digits e) + = matches one or more of the previous thing. (In this case the digits) f) $ = matches end of the line.

However I'm having a hard time putting everything together. Does it only want matches that start or end with digits but where do - and ? come in?

  1. Can this regex return a null? From different sources I see
Source A:

s.matches("regex") evaluates if "regex" matches s. Returns only true if the WHOLE string can be matched.
Source B:

If the regular expression matches the whole text, then the matches() method returns true. If not, the matches() method returns false.

You cannot use the matches() method to search for multiple occurrences of a regular expression in a text. For that, you need to use the find(), start() and end() methods.

It doesn't explicitly state that under no condition can a null be returned but my understanding is that it can only return True or False? If that is the case, why does the NullPointerException point to the isInteger function?

Note: I've checked line 255 within the function createChatNotification and it's a call to the function isInteger.

private void createChatNotification(String argA, final String argB, final String argC) {

        if (DataParsingUtils.isInteger(argA)) {
Mark
  • 3,138
  • 5
  • 19
  • 36
  • 2
    I'd say the parameter `s` is null. Can you show how you call `isInteger`? The regex itself looks fine. – sp00m Dec 10 '19 at 18:22
  • 2
    The error message says "`Attempted to invoke matches(String) on a null reference`". That suggest that `s` is null, which is by far the simplest explanation anyway. – khelwood Dec 10 '19 at 18:25
  • I see, thank you guys, I'll dig deeper in that respect. – Mark Dec 10 '19 at 18:28

0 Answers0