automation error - creating many polygon labels

Discussion created by dorisriedl on Jul 3, 2012
I want to create polygon labels for about 40.000 polygons with all the polygon data copied to the point in ArcMap 10 SP4.
I use the  CalculatePolygonCentroid.getCentroids method below to calculate the labels. But I only succeed to calculate about 13500 and then I get  "Automation Exception: One or more arguments are invalid".
I used the Windows Resource Monitor to observe the CPU and Memory, but nothing seemed conspicuous (no value came near 100).

I also tried several other versions of the code below, e.g. with an Insert Cursor, but this only changed the amount of calculated labels minimally (I always stayed between 12735 and 13700).
I tried to work with a QueryFilter to only execute a subset of (5000 or 1000) and release the Cursor every time, before starting on the new set -> no substantial change.
I tried within an Edit Session - even stopping and starting editing after every subset and outside an Edit Session.

The polygon dataset has no topology errors, I checked this thoroughly.

import javax.swing.JOptionPane;

import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.Feature;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geometry.Polygon;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geodatabase.IQueryFilter;

import com.esri.arcgis.system.Cleaner;

public class CalculatePolygonCentroid {
    IFeatureClass polygons;
    IFeatureClass centroids;
    IFields centroidFields;
    public CalculatePolygonCentroid(IFeatureClass polygons, IFeatureClass centroids, IFields centroidFields){
        this.polygons = polygons;
        this.centroids = centroids;
        this.centroidFields = centroidFields;
    public void getCentroids(){
            IFeature feature;
            Polygon polygon;
            Point centroid;
            Feature pointFeature;

            int j;
            int[][]compare = mapFields();
            IFeatureCursor polygonsCursor =, false);
            while ((feature = polygonsCursor.nextFeature()) != null){
                polygon = (Polygon) feature.getShapeCopy();
                centroid = (Point) polygon.getLabelPoint();
                pointFeature = (Feature)centroids.createFeature();
                for(j = 0; j < compare.length; j++){
                    if (pointFeature.getFields().getField(compare[j][0]).isEditable() 
                            && pointFeature.getFields().getField(compare[j][0]).getType() != esriFieldType.esriFieldTypeGeometry){
                        pointFeature.setValue(compare[j][0], feature.getValue(compare[j][1]));
        }catch (Exception e){
            JOptionPane.showMessageDialog(null, "Can't get Centroids! "+  e.getMessage());
    private int[][] mapFields(){
        try {
            IFields polyFields = polygons.getFields();
            IFields centroidFields = centroids.getFields();
            int [][] fieldsMap = new int [centroidFields.getFieldCount()][2];
               String polyfname;
               String centroidfname;
                for (int i = 0; i < fieldsMap.length; i++){
                    for (int j = 0; j < polyFields.getFieldCount(); j++){
                        centroidfname = centroidFields.getField(i).getName();
                        polyfname = polyFields.getField(j).getName();
                        if (centroidfname.equals(polyfname)){
                                fieldsMap[i][0] = i;
                                fieldsMap[i][1] = j;
            return fieldsMap;
        }catch (Exception e){
            JOptionPane.showMessageDialog(null, "Can't get Fieldmap!");
            int [][] fieldsMap = new int [1][2];
            fieldsMap [0][0]= 0;
            fieldsMap [0][1]= 0;
            return fieldsMap;


Do you have any ideas what I do wrong?

I am already a bit desperate by now, because it seems a straightforward enough task to do.