0

I am trying to run local tests to test an algorithm I have for detecting certain URL's from a source string.

I have a class, CalendarUtil, that interfaces with the users calendar and parses their meeting information from the title, location and description fields.

Instead of doing an integration test, I decided to go local since I just want to test this algorithm with hard-coded input to make sure I am getting expected results without relying on the other Android API's.

CalendarUtil acts like a model class in that it has references to the meetings 3 fields mentioned above, so in my @Before setUp() method I create an object and only set those three fields with appropriate strings.

I then created a @Test function for parsing URL's and this is the stack trace of the error:

java.lang.NullPointerException
at com.myapp.utils.StringUtil.getStringMatches(StringUtil.java:94)
at com.myapp.utils.StringUtil.extractUrls(StringUtil.java:26)
at com.myapp.utils.CalendarUtil.parseAndRemoveUrls(CalendarUtil.java:186)
at com.myapp.MeetingDetectionTests.parseWebexUrl(MeetingDetectionTests.java:42)

At line 94 inside StringUtil this is the method:

/**
 * Generic function that will take in a source that will be parsed according to a provided Regex pattern
 *
 * @param source  The string to parse
 * @param pattern The regex pattern to use
 * @return A HashMap with the key being the match from the source and the values being the NSRange of where the match was found in the source.
 */
private static HashMap<String, NSRange> getStringMatches(final String source, final Pattern pattern) {
    Matcher matcher = pattern.matcher(source); //Line 94, NullPointerException

    //The key is our match in the source; the values is an array of integers representing the start and end index of the match
    HashMap<String, NSRange> retVal = new HashMap<>();

    while (matcher.find())
        //Add the match as a key to our hashmap with an NSRange indicated where in the string it was found
        retVal.put(matcher.group(), new NSRange(matcher.start(), matcher.end() - matcher.start()));

    return retVal;
}

The passed value for the pattern is Patterns.WEB_URL which is an android provided regex for URL's. It is showing up as null in my test, but works when running my app.

Any ideas what is happening?

romero-ios
  • 1,075
  • 1
  • 11
  • 20
  • Based on what you've provided, the only conclusion that can be made `pattern` is null. You'll need to step through the code to find out what's happening or provide a complete example. – Christopher Schneider May 17 '17 at 14:27
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – EJoshuaS - Stand with Ukraine May 17 '17 at 14:38
  • 2
    In my application, pattern is not null, i am passing Patterns.WEB_URL. Only in my unit test is comes up as null; I found this out by stepping through the application. – romero-ios May 17 '17 at 14:39

0 Answers0