Geotools快速入门教程JFileDataStoreChooser.showOpenFile导致错误

时间:2011-10-12 21:37:59

标签: java eclipse swing geotools

我正在使用Eclipse查看Geotools快速入门教程 http://docs.geotools.org/latest/userguide/tutorial/quickstart/eclipse.html 我按照所有步骤操作,一切顺利,直到我运行Quickstart类

当我运行Quickstart类时。我收到了这个错误

Exception in thread "main" java.lang.NullPointerException
    at sun.awt.shell.Win32ShellFolder2.getFileSystemPath(Win32ShellFolder2.java:571)
    at sun.awt.shell.Win32ShellFolder2.access$400(Win32ShellFolder2.java:72)
    at sun.awt.shell.Win32ShellFolder2$2.call(Win32ShellFolder2.java:298)
    at sun.awt.shell.Win32ShellFolder2$2.call(Win32ShellFolder2.java:296)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at sun.awt.shell.Win32ShellFolderManager2$ComInvoker$3.run(Win32ShellFolderManager2.java:502)
    at java.lang.Thread.run(Thread.java:722)

我发现这个方法导致问题JFileDataStoreChooser.showOpenFile

我注释掉除了使用上述方法的所有其他行,并且仍然得到相同的错误。 (如果我也评论该行,则没有错误)

package org.geotools.tutorial;

import java.io.File;

import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;

/**
 * Prompts the user for a shapefile and displays the contents on the screen in a map frame.
 * <p>
 * This is the GeoTools Quickstart application used in documentationa and tutorials. *
 */
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
          /*********This is the line *********/
          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);
    }

}

另外,在pom.xml中,我尝试了8.0-M2和8-SNAPSHOT版本。但没有运气......

任何人都有任何想法? 模块geotools.swing有什么问题吗?

由于

2 个答案:

答案 0 :(得分:1)

可能是sun.awt.shell.Win32ShellFolder2.getFileSystemPath方法触发NPE的错误。

有人建议使用JDK 1.6.0u21或更高版本来解决它。

使用关键字"NullPointerException sun.awt.shell.Win32ShellFolder2"

检查Google

答案 1 :(得分:1)

我一直在尝试Geotools快速入门教程,我遇到了同样的问题。

我终于补充道:

import org.geotools.data.shapefile.*;

并更改

FileDataStore store = FileDataStoreFinder.getDataStore(file);

通过

ShapefileDataStore store = new ShapefileDataStore(file.toURI().toURL());

它不能解决异常问题,但它可以作为替代方案。

相关问题