From eb7a59137b34aaa02ce5c17efba0ead940d18198 Mon Sep 17 00:00:00 2001 From: Sean Barbeau Date: Fri, 9 Sep 2016 22:10:11 -0400 Subject: [PATCH] Fix #308 - Add null check for removing item from uninitialized QuadTree --- .../maps/android/quadtree/PointQuadTree.java | 6 +++++- .../android/quadtree/PointQuadTreeTest.java | 19 ++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/library/src/com/google/maps/android/quadtree/PointQuadTree.java b/library/src/com/google/maps/android/quadtree/PointQuadTree.java index 8ecad6ddf..f87502f4f 100644 --- a/library/src/com/google/maps/android/quadtree/PointQuadTree.java +++ b/library/src/com/google/maps/android/quadtree/PointQuadTree.java @@ -173,7 +173,11 @@ private boolean remove(double x, double y, T item) { } } else { - return mItems.remove(item); + if (mItems == null) { + return false; + } else { + return mItems.remove(item); + } } } diff --git a/library/tests/src/com/google/maps/android/quadtree/PointQuadTreeTest.java b/library/tests/src/com/google/maps/android/quadtree/PointQuadTreeTest.java index 747204d56..88b2590e2 100644 --- a/library/tests/src/com/google/maps/android/quadtree/PointQuadTreeTest.java +++ b/library/tests/src/com/google/maps/android/quadtree/PointQuadTreeTest.java @@ -45,7 +45,13 @@ public void testEmpty() { } public void testMultiplePoints() { + boolean response; Item item1 = new Item(0, 0); + + // Remove item that isn't yet in the QuadTree + response = mTree.remove(item1); + assertFalse(response); + mTree.add(item1); Item item2 = new Item(.1, .1); mTree.add(item2); @@ -59,11 +65,18 @@ public void testMultiplePoints() { assertTrue(items.contains(item2)); assertTrue(items.contains(item3)); - mTree.remove(item1); - mTree.remove(item2); - mTree.remove(item3); + response = mTree.remove(item1); + assertTrue(response); + response = mTree.remove(item2); + assertTrue(response); + response = mTree.remove(item3); + assertTrue(response); assertEquals(0, searchAll().size()); + + // Remove item that is no longer in the QuadTree + response = mTree.remove(item1); + assertFalse(response); } public void testSameLocationDifferentPoint() {