ArcObjects Java 10.0 sp5If an IGroupLayer is composed of IFeatureLayers, the following code will print the IGroupLayer name.If I pass a name that I *think* is an IGroupLayer, my method crashes with null pointer. So, if you have a map document with a mosaic with sub layers: Boundary, Footprint and Image, is that not an IGroupLayer? I've also tried ICompositeLayer also, with the same null pointer result.If I pass the name of my mosaic layer to the following method, I get a null pointer. If pass the name of any other group layer, it will print the parent group name, in this example "Transportation".
public IGroupLayer getGroupLayerName(com.esri.arcgis.carto.Map map, AGSConnection agsconn){
ILayer layer = null;
try {
UID id = (UID) agsconn.getContext().createObject(UID.getClsid());
id.setValue(com.esri.arcgis.carto.IGroupLayer.class);
// getLayers(UID, true) uses recursion to the get the layer name in a GroupLayer
IEnumLayer enumLayer = map.getLayers(id, true);
enumLayer.reset();
layer = enumLayer.next();
while(!(layer == null)){
if(layer.getName().equalsIgnoreCase("Transportation")){
break;
} else {
layer = enumLayer.next();
}
}
} catch (AutomationException ae) {
ae.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println(layer.getName());
} catch (AutomationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return (IGroupLayer) layer;
}
This method can find the layer named "Image" located within the Mosaic layer, but I really wanted the name of the mosaic group layer.
public IRasterLayer getRasterLayerName(com.esri.arcgis.carto.Map map, AGSConnection agsconn){
ILayer layer = null;
try {
UID id = (UID) agsconn.getContext().createObject(UID.getClsid());
id.setValue(com.esri.arcgis.carto.IRasterLayer.class);
// getLayers(UID, true) uses recursion to the get the layer name in a GroupLayer
IEnumLayer enumLayer = map.getLayers(id, true);
enumLayer.reset();
layer = enumLayer.next();
while(!(layer == null)){
if(layer.getName().equalsIgnoreCase("Image")){
break;
} else {
layer = enumLayer.next();
}
}
} catch (AutomationException ae) {
ae.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return (IRasterLayer) layer;
}