public class EngineTest { @SuppressWarnings("deprecation") public static void main(String[] args) { try { //Step 1: Initialize the Java Componet Object Model (COM) Interop. EngineInitializer.initializeEngine(); //Step 2: Initialize a valid license. new AoInitialize().initialize (esriLicenseProductCode.esriLicenseProductCodeEngine); //Step 3 : Invoke ArcObjects. MapDocument mapDocument = new MapDocument(); if (mapDocument.isPresent("D:\\Tellus_Sedimentary_Basin.mxd")) { mapDocument.open("D:\\Tellus_Sedimentary_Basin.mxd",""); mapDocument.setAuthor("Brian Varela"); IMap main_map = mapDocument.getMap(0); IFeatureLayer layer = (IFeatureLayer) main_map.getLayer(0); IFeatureClass classLayer = layer.getFeatureClass(); IDataLayer2 dataLayer = (IDataLayer2) layer; IDatasetName name = (IDatasetName) dataLayer.getDataSourceName(); IWorkspaceName workspace = name.getWorkspaceName(); ListLayers((Map)mapDocument.getMap(0)); System.out.println("Layer "+layer.getName()+" located at "+workspace.getPathName()+" "+layer.getDisplayField()); } } catch (IOException ex) { System.out.println(ex.getMessage()); System.out.println("App failed."); } finally { try { //Step 4: Release the license. new AoInitialize().shutdown(); System.out.println("License released!"); } catch (IOException ex) { ex.printStackTrace(); } } } //End of method main. public static void ListLayers(com.esri.arcgis.carto.Map map) { try{ for(int i=0; i<map.getLayerCount(); i++) { System.out.println("Layer "+i+". "+map.getLayer(i).getName()); } }catch(AutomationException ae) { ae.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } } } //End of class
Solved! Go to Solution.
Well, keep at it. Sooner or later you will debug your code and realize that the method is not the problem.
In your code, what is to the left of this line? IFeatureLayer or FeatureLayer?listAttributes((FeatureLayer) main_map.getLayer(0))
public static void listAttributes(com.esri.arcgis.carto.FeatureLayer fl) { try { IFeatureClass featureClass = fl.getFeatureClass(); IFeatureCursor updateCursor = featureClass.search(null, false); int theFieldIndex = featureClass.findField("BASIN_NAME"); IFeature feature = null; while((feature = updateCursor.nextFeature()) != null) { System.out.println( (String)feature.getValue(theFieldIndex)); } } catch (AutomationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
the listAttributes method I created doesn't return anything and only takes a FeatureLayer as an import
public class EngineTest { @SuppressWarnings("deprecation") public static void main(String[] args) { try { //Step 1: Initialize the Java Componet Object Model (COM) Interop. EngineInitializer.initializeEngine(); //Step 2: Initialize a valid license. new AoInitialize().initialize (esriLicenseProductCode.esriLicenseProductCodeEngine); //Step 3 : Invoke ArcObjects. MapDocument mapDocument = new MapDocument(); if (mapDocument.isPresent("D:\\Tellus_Sedimentary_Basin.mxd")) { mapDocument.open("D:\\Tellus_Sedimentary_Basin.mxd",""); mapDocument.setAuthor("Brian Varela"); IMap main_map = mapDocument.getMap(0); IFeatureLayer layer = (IFeatureLayer) main_map.getLayer(0); IFeatureClass classLayer = layer.getFeatureClass(); IDataLayer2 dataLayer = (IDataLayer2) layer; IDatasetName name = (IDatasetName) dataLayer.getDataSourceName(); IWorkspaceName workspace = name.getWorkspaceName(); //System.out.println("Number of Maps in this document: "+mapDocument.getMapCount()); System.out.println("SUCCESS "+mapDocument.getDocumentFilename()+" ArcMap file opened - Author: "+mapDocument.getAuthor()); ListLayers((Map)mapDocument.getMap(0)); System.out.println("Opened Layer "+layer.getName()+" located at "+workspace.getPathName()+" "+layer.getDisplayField()); FeatureLayer fl = (FeatureLayer) getLayerByName(main_map, layer.getName()); listAttributes(fl); } } catch (IOException ex) { System.out.println(ex.getMessage()); System.out.println("App failed."); } finally { try { //Step 4: Release the license. new AoInitialize().shutdown(); System.out.println("License released!"); } catch (IOException ex) { ex.printStackTrace(); } } } //End of method main. public static void listAttributes(com.esri.arcgis.carto.FeatureLayer fl) { try { IFeatureClass featureClass = fl.getFeatureClass(); IFeatureCursor updateCursor = featureClass.search(null, false); int theFieldIndex = featureClass.findField("BASIN_NAME"); IFeature feature = null; while((feature = updateCursor.nextFeature()) != null) { System.out.println( (String)feature.getValue(theFieldIndex)); } } catch (AutomationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void ListLayers(com.esri.arcgis.carto.Map map) { try{ for(int i=0; i<map.getLayerCount(); i++) { System.out.println("Layer "+i+". "+map.getLayer(i).getName()); } }catch(AutomationException ae) { ae.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } } /** * ESRI sample method to get feature class layer by name, instead of index. * @param layerName the layer name as <code>String</code>. * @return layer as <code>FeatureLayer</code>. */ public static FeatureLayer getLayerByName(IMap s_iMap, String layerName) { FeatureLayer layer = null; try { for (int i = 0; i < s_iMap.getLayerCount(); i++) { if (s_iMap.getLayer(i).getName().equalsIgnoreCase(layerName)) { layer = (FeatureLayer) s_iMap.getLayer(i); break; } } } catch (AutomationException ae) { ae.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // Returns the featurelayer return layer; } }
IFeatureClass featureClass = fl.getFeatureClass();
IFeatureClass featureClass = fl.getFeatureClass();
This line is your problem, but stems from another issue. The featureClass variable in listAtributes is null, so when you try to call search on featureClass, you're getting the null pointer exception.
Why is featureClass null?
Open a DBase (dbf) file In this case, pathToFile is the path to the folder holding the dbf file. Additionally, the file extension, .dbf, must be included when specifying the name of the DBase file. [Java] ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory(); Workspace workspace = new Workspace(shapefileWorkspaceFactory.openFromFile(pathToFile,0)); Table table = new Table(workspace.openTable(fileName));
public static void ArcDBF(String path, String fileName) { try{ ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory(); Workspace workspace = new Workspace(shapefileWorkspaceFactory.openFromFile(path,0)); Table table = new Table(workspace.openTable(fileName)); System.out.println("Success!"); }catch(AutomationException ae) { ae.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } }
mapDocument.open("D:\\Tellus_Sedimentary_Basin.mxd",""); IMap main_map = mapDocument.getMap(0); IFeatureLayer layer = (IFeatureLayer) main_map.getLayer(0); IDataLayer2 dataLayer = (IDataLayer2) layer; IDatasetName name = (IDatasetName) dataLayer.getDataSourceName(); IWorkspaceName workspace = name.getWorkspaceName(); ArcDBF(workspace.getPathName()+"\\", "Tellus_Sedimentary_Basins_Layer.dbf");
at com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory.openFromFile(Unknown Source) at EngineTest.ArcDBF(EngineTest.java:113) at EngineTest.main(EngineTest.java:69) Unhandled Exception: Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'ESRI.DADF.Core.dll' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)) File name: 'ESRI.DADF.Core.dll' System.IO.FileLoadException: Could not load file or assembly 'ESRI.DADF.Core.dll' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)) File name: 'ESRI.DADF.Core.dll'
Workspace workspace = new Workspace(shapefileWorkspaceFactory.openFromFile(path,0));