A couple things are odd here, one of which is using GeoTools 13-RC1 :)
Release candidates are generally used as an initial sanity check prior to a
release - and are not intended for production use. The second is using an
older copy of GeoTools ... GeoTools 13 has one more scheduled release
before it is archived.

Other than that lets look at where the stack trace occurs line
ShapefileFeatureSource:236
<https://github.com/geotools/geotools/blob/12-RC1/modules/plugin/shapefile/src/main/java/org/geotools/data/shapefile/ShapefileFeatureSource.java#L236>
(this
link is to github):

        } else if (getDataStore().isIndexed() && !bbox.isNull()
                && !Double.isInfinite(bbox.getWidth()) &&
!Double.isInfinite(bbox.getHeight())) {
            try {
                if(indexManager.isSpatialIndexAvailable() ||
getDataStore().isIndexCreationEnabled()) {
                    goodRecs = indexManager.querySpatialIndex(bbox);
                }
            } catch (TreeException e) {
                throw new IOException("Error querying index: " +
e.getMessage());
            }
        }

So either getDataStore is null, or bbox is null.

Is there a chance that your shapefile is empty (or we were otherwise unable
to generate a useful bounds?)

The bounds is calculated a few lines earlier:

        // grab the target bbox, if any
        Envelope bbox = new ReferencedEnvelope();
        if (q.getFilter() != null) {
            bbox = (Envelope)
q.getFilter().accept(ExtractBoundsFilterVisitor.BOUNDS_VISITOR, bbox);
        }

You can try the debugger, or ExtractBoundsFilterVisitor.BOUNDS_VISITOR in
your own code to see if it is returning a null bounds.





--
Jody Garnett

On 24 July 2015 at 03:24, Mirko Reul <pub...@reul.ws> wrote:

> Hello Geotools users,
>
> I am trying to complete the Geotools map selection tutorial with the
> code from the guide at:
> http://docs.geotools.org/stable/userguide/tutorial/map/style.html#selection
>
> After importing (virtually any) shapefile and activating the selection
> tool, a click on any of the Features results in a NullPointerException.
>
> Now I am wondering whether I am the only one getting that error or if
> there is a bug in the example code (that I can't find).
>
> I'm working with Eclipse (Maven plugin), using Geotools 12-RC1,
> environment is OpenJDK 1.7.0_79. I was able to reproduce the error with
> Geotools 13.1 as well. Stacktrace:
>
> java.lang.NullPointerException
>         at
>
> org.geotools.data.shapefile.ShapefileFeatureSource.getReaderInternal(ShapefileFeatureSource.java:236)
>         at
>
> org.geotools.data.shapefile.ShapefileFeatureStore.getReaderInternal(ShapefileFeatureStore.java:126)
>         at
>
> org.geotools.data.store.ContentFeatureSource.getReader(ContentFeatureSource.java:563)
>         at
>
> org.geotools.data.store.ContentFeatureCollection.features(ContentFeatureCollection.java:164)
>         at
>
> org.geotools.tutorial.style.SelectionLab.selectFeatures(SelectionLab.java:211)
>         at
>
> org.geotools.tutorial.style.SelectionLab$1$1.onMouseClicked(SelectionLab.java:157)
>         at
>
> org.geotools.swing.event.DefaultMapMouseEventDispatcher.mouseClicked(DefaultMapMouseEventDispatcher.java:99)
>         at
> java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
> [...]
>
> The error appears to be related to the selectFeatures method:
>
>     void selectFeatures(MapMouseEvent ev) {
>
>         System.out.println("Mouse click at: " + ev.getWorldPos());
>
>         /*
>          * Construct a 5x5 pixel rectangle centred on the mouse click
> position
>          */
>         Point screenPos = ev.getPoint();
>         Rectangle screenRect = new Rectangle(screenPos.x-2,
> screenPos.y-2, 5, 5);
>
>         /*
>          * Transform the screen rectangle into bounding box in the
> coordinate
>          * reference system of our map context. Note: we are using a
> naive method
>          * here but GeoTools also offers other, more accurate methods.
>          */
>
>         AffineTransform screenToWorld =
> mapFrame.getMapPane().getScreenToWorldTransform();
>         Rectangle2D worldRect =
> screenToWorld.createTransformedShape(screenRect).getBounds2D();
>         ReferencedEnvelope bbox = new ReferencedEnvelope(
>                 worldRect,
> mapFrame.getMapContent().getCoordinateReferenceSystem());
>
>         /*
>          * Create a Filter to select features that intersect with
>          * the bounding box
>          */
>         Filter filter =
> ff.intersects(ff.property(geometryAttributeName), ff.literal(bbox));
>
>         /*
>          * Use the filter to identify the selected features
>          */
>         try {
>             SimpleFeatureCollection selectedFeatures =
>                     featureSource.getFeatures(filter);
>
>             SimpleFeatureIterator iter = selectedFeatures.features();
>             Set<FeatureId> IDs = new HashSet<FeatureId>();
>             try {
>                 while (iter.hasNext()) {
>                     SimpleFeature feature = iter.next();
>                     IDs.add(feature.getIdentifier());
>
>                     System.out.println("   " + feature.getIdentifier());
>                 }
>
>             } finally {
>                 iter.close();
>             }
>
>             if (IDs.isEmpty()) {
>                 System.out.println("   no feature selected");
>             }
>
>             displaySelectedFeatures(IDs);
>
>         } catch (Exception ex) {
>             ex.printStackTrace();
>             return;
>         }
>     }
>
> Thanks a lot in advance!
>
> Regards,
> Mirko
>
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> GeoTools-GT2-Users mailing list
> GeoTools-GT2-Users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
>
>
------------------------------------------------------------------------------
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to