2017-06-01 201 views
1

我是WFS和geotools的新手。我試圖連接到LRIS WFS。嘗試連接到WFS時發生NullPointerException

代碼如下:

String getCapabilities = "https://lris.scinfo.org.nz/services;key=ENTER-KEY/wfs/layer-66?service=WFS&request=GetCapabilities"; 

Map connectionParameters = new HashMap(); 
connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities); 

// Step 2 - connection  
DataStore data = DataStoreFinder.getDataStore(connectionParameters); 

// Step 3 - discouvery 
String typeNames[] = data.getTypeNames(); 

我得到一個空指針異常就行:

String typeNames[] = data.getTypeNames(); 

任何幫助表示讚賞。謝謝。

回答

0

最佳做法是在使用之前檢查DataStore是否已打開(即不是空)。

與下面的代碼我找回數據很容易地說,我假定你已經添加你自己的關鍵網址:

String getCapabilities = "https://lris.scinfo.org.nz/services;key=myKEY/wfs/layer-66?service=WFS&request=GetCapabilities"; 

    Map connectionParameters = new HashMap(); 
    connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities); 
    connectionParameters.put("WFSDataStoreFactory:WFSDataStoreFactory:TIMEOUT", 10000000); 
    // Step 2 - connection 
    DataStore data = DataStoreFinder.getDataStore(connectionParameters); 
    if(data==null) { 
     System.err.println("failed to open datastore at "+getCapabilities); 
    } 
    // Step 3 - discouvery 
    String types[] = data.getTypeNames(); 
    for (int i = 0; i < types.length; i++) { 
     System.out.println(types[i]); 

     String name = types[i]; 
     Query query = new Query(name); 
     query.setCoordinateSystem(DefaultGeographicCRS.WGS84); 
     SimpleFeatureSource source = data.getFeatureSource(name); 
     SimpleFeatureType schema = source.getSchema(); 
     System.out.println(schema); 
     SimpleFeatureCollection fc = source.getFeatures(query); 
     int count = 0; 
     while (fc.features().hasNext() && (count++ < 10)) { 
     SimpleFeature sf = fc.features().next(); 
     System.out.println(sf); 
     } 
    } 
    } 

請注意,我也不得不調高超時參數作爲新西蘭在英國離我很遠。

相關問題