Porting a Network trace C# tool to Java.

731
2
02-10-2011 06:07 AM
ReneRubalcava
Frequent Contributor
I'm diving head first into learning some Java using ArcObjects.
We have a Flow trace tool using a geometric network that we have written in C# and it works perfectly on the desktop and on server.

I have had some pretty good success in porting most of the code over to Java, but I have hit a snag in trying to cast a FeatureDataset to a NetworkCollection.

This is the working portion in C#.
// Obtain a reference to the geometric network in the feature workspace
IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset("sde.Sewerage_Network_Classes");
INetworkCollection networkCollection = (INetworkCollection)featureDataset;
IGeometricNetwork geometricNetwork = networkCollection.get_GeometricNetworkByName("sde.Sewerage_Network_Classes_Net");

INetwork network = geometricNetwork.Network;


Looks pretty much the exact same in Java
// Obtain a reference to the geometric network in the feature workspace
INetwork network = null;
INetSolver netSolver = null;
IGeometricNetwork geometricNetwork = null;
INetworkCollection networkCollection = null;
try
{
 IFeatureDataset featureDataset = featureWorkspace.openFeatureDataset("sde.Sewerage_Network_Classes");
 networkCollection = (INetworkCollection) featureDataset; // <-- this is where the error happens
 geometricNetwork = networkCollection.getGeometricNetworkByName("sde.Sewerage_Network_Classes_Net");
}
catch(Exception e) 
{
 System.out.println("This error sucks!");
 System.out.println(e.getMessage());
}


I'm running my jUnit tests and catching the following error at the line indicated above.
com.esri.arcgis.geodatabase.IFeatureDatasetProxy cannot be cast to com.esri.arcgis.geodatabase.INetworkCollection


I'm using the same connection properties to connect to the SDE, using the default instance.
I'm pretty much stumped at this point. Is there a nuance that I might be missing?

Thanks in advance.
0 Kudos
2 Replies
zarfishanzahid
New Contributor
You can use this C# to Java Converter cloud application tool to port your c# application to java instantly. Its free and very easy to use and doesnt required any installation or download.
0 Kudos
LeoDonahue
Occasional Contributor III
Rene,

This is your problem:

IFeatureDataset featureDataset = featureWorkspace.openFeatureDataset("sde.Sewerage_Network_Classes");


I'm assuming that your featureWorkspace is a com.esri.arcgis.geodatabase.Workspace and if you look at the openFeatureDataset() method you'll notice that it returns an IFeatureDataset, not a FeatureDataset, which is why you're getting a IFeatureDatasetProxy, which is noncastable according to the docs.

Does featureDataset need to be declared as an Interface?

Or you could cast your featureDataset to a real FeatureDataset and then use that in place of the cast to INetworkCollection.
0 Kudos