Uploaded image for project: 'Commons Collections'
  1. Commons Collections
  2. COLLECTIONS-586

PatriciaTrie prefixMap clear throws NullPointerException

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.1
    • None
    • None
    • None

    Description

      Clearing all entries of a prefixMap returned by PatriciaTrie using the clear method throws a NullPointerException. The workaround of removing each entry using the remove method seems to work.

      Here are the test cases for the bug and the workaround:

      public class PatriciaTrieTest {
      
          private Trie<String, Integer> trie;
      
          @Before
          public void setUp() {
              trie = new PatriciaTrie<Integer>();
              trie.put("Anna", 1);
              trie.put("Anael", 2);
              trie.put("Analu", 3);
              trie.put("Andreas", 4);
              trie.put("Andrea", 5);
              trie.put("Andres", 6);
              trie.put("Anatole", 7);
          }
      
          @Test
          public void testPrefixMapClear() {
              SortedMap<String, Integer> prefixMap = trie.prefixMap("And");
              assertEquals(new HashSet<>(Arrays.asList("Andrea", "Andreas", "Andres")), prefixMap.keySet());
              assertEquals(Arrays.asList(5, 4, 6), new ArrayList<>(prefixMap.values()));
      
              prefixMap.clear();
              assertTrue(prefixMap.keySet().isEmpty());
              assertTrue(prefixMap.values().isEmpty());
              assertEquals(new HashSet<>(Arrays.asList("Anael", "Analu", "Anatole", "Anna")), trie.keySet());
              assertEquals(Arrays.asList(2, 3, 7, 1), new ArrayList<>(trie.values()));
          }
      
          @Test
          public void testPrefixMapClearUsingRemove() {
              SortedMap<String, Integer> prefixMap = trie.prefixMap("And");
              assertEquals(new HashSet<>(Arrays.asList("Andrea", "Andreas", "Andres")), prefixMap.keySet());
              assertEquals(Arrays.asList(5, 4, 6), new ArrayList<>(prefixMap.values()));
      
              Set<String> keys = new HashSet<String>(prefixMap.keySet());
              for (final String key : keys) {
                  prefixMap.remove(key);
              }
              assertTrue(prefixMap.keySet().isEmpty());
              assertTrue(prefixMap.values().isEmpty());
              assertEquals(new HashSet<>(Arrays.asList("Anael", "Analu", "Anatole", "Anna")), trie.keySet());
              assertEquals(Arrays.asList(2, 3, 7, 1), new ArrayList<>(trie.values()));
          }
      
      }
      

      The stacktrace of the NullPointerException thrown by the testPrefixMapClear test case is:

      java.lang.NullPointerException
      	at org.apache.commons.collections4.trie.AbstractPatriciaTrie$PrefixRangeEntrySet$EntryIterator.remove(AbstractPatriciaTrie.java:2370)
      	at java.util.AbstractCollection.clear(AbstractCollection.java:432)
      	at java.util.AbstractMap.clear(AbstractMap.java:288)
      	at PatriciaTrieTest.testPrefixMapClear(PatriciaTrieTest.java:39)
      

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            shailender.bathula@tvnz.co.nz Shailender Bathula
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment