I have a problem with getting the ID of a newly created feature in the shapefile.

I have a datasource / datastore that reads and writes to a shapefile. I create a feature, add it to the store and commit transaction. The problem is, that as a result of my method I want to have my newly created feature in a geojson format. Everything is fine except the ID of the new feature which is "new0" instead of the FID that actually appears in the shapefile.

This is the piece of code I use to add feature to the store:

FeatureWriter<SimpleFeatureType, SimpleFeature> writer = // I get my writer here ...
SimpleFeature feature = writer.next();

The feature is created and stored in the shp. Only the problem remains with the FID of the appended feature. When I run exactly the same method for SDE datastore it works as expected.

I have seen this error on other forums. The posts were even asked quite long time ago and I hoped that this bug will be fixed in Geotools 13. Apparently it is not :(

Is there a way to fix it somehow ?

Before changing to Geotools 13 I used the version: 12.2. There I made this listener class:

private static class CreatedFeatureListener implements FeatureListener {
    private Filter                                                  filter;
    private FeatureSource<? extends FeatureType, ? extends Feature> source;

    public void changed(FeatureEvent featureEvent) {
        filter = featureEvent.getFilter();
        source = featureEvent.getFeatureSource();

    public List<Feature> getAddedFeatures() throws IOException {
        List<Feature> result = new ArrayList<>();
        FeatureIterator<? extends Feature> iter = null;
        try {
            iter = source.getFeatures(filter).features();
            while (iter.hasNext()) {
        } finally {
            if (iter != null) {
        return result;

It allowed me to properly get the new feature with the proper ID. Unfortunately after changing the version it stopped working. The event is not fired and I get NullPointerException when I call getAddedFeatures() method.

1 Answer 1


FIDs are unique to a feature in a datastore. Thus you shouldn't be setting the FID yourself, especially not when dealing with a shapefile where they are hard coded.

The correct answer if you need to refer to the same feature in different datastores is to provide your own attribute that you control which will be the same in each place that you store or serialise the feature.

I can't see any evidence for FeatureEvents having changed in the past 4 years - https://github.com/geotools/geotools/tree/master/modules/library/api/src/main/java/org/geotools/data so I'm not sure why that code would suddenly fail. Maybe you should check for a null source and possibly which type of event you got.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.