0

I am new to gis based project. I am using netbeans IDE to include my shapefiles and i am sure that I have imported necessary jars to include shapefile. but its not working .when i run my application i am getting an null pointer exception in the simplefeaturesource method . Hereby i include my program

public class Quickstart {

    /**
     * GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its
     * contents on the screen in a map frame
     */
    public static void main(String[] args) throws Exception {
        // display a data store file chooser dialog for shapefiles
        File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }

        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        SimpleFeatureSource featureSource = store.getFeatureSource();

        // Create a map content and add our shapefile to it
        MapContent map = new MapContent();
        map.setTitle("Quickstart");

        Style style = SLD.createSimpleStyle(featureSource.getSchema());
        Layer layer = new FeatureLayer(featureSource, style);
        map.addLayer(layer);

        // Now display the map
        JMapFrame.showMap(map);
    }
}
Iswanto San
  • 18,263
  • 13
  • 58
  • 79

1 Answers1

0

According to the function or method definition I have found:

public static FileDataStore getDataStore(File file)
                                  throws IOException

Checks each available datasource implementation in turn and returns the first one which claims to support the given file..

Parameters:

  • file - the file

Returns:
The first datasource which claims to process the required resource, returns null if none can be found.

Now in the instance that it fails, it will return null in which case store will equal null in the code above. Since store is not checked with an if statement to say if it is not null proceed but merely declared and initialised and you just hope for the best, the next line is then producing a null pointer exception.

That is my take on your code. Try and debug it and see if store equals null or not or use an if statement on the line before.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Soliman Soliman
  • 159
  • 1
  • 4
  • 17