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

PatriciaTrie prefixMap clear throws NullPointerException

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 4.1
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      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

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: