0

我是基于 gis 的项目的新手。我正在使用 netbeans IDE 来包含我的 shapefile,并且我确信我已经导入了必要的 jar 来包含 shapefile。但它不起作用。当我运行我的应用程序时,我在 simplefeaturesource 方法中得到一个空指针异常。在此我包括我的程序

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);
    }
}
4

1 回答 1

0

根据我发现的函数或方法定义

public static FileDataStore getDataStore(File file)
                                  throws IOException

依次检查每个可用的数据源实现并返回第一个声称支持给定文件的实现。

参数:

  • 文件 - 文件

返回:
声称处理所需资源的第一个数据源,如果找不到,则返回 null。

现在在它失败的情况下,它将返回 null 在这种情况下,store 将在上面的代码中等于 null。由于未使用 if 语句检查 store 以判断它是否为 null 继续进行,而只是声明和初始化,并且您只希望获得最好的结果,因此下一行将产生空指针异常。

这是我对你的代码的看法。尝试调试它,看看 store 是否等于 null 或在前面的行上使用 if 语句。

于 2013-04-17T08:13:43.890 回答