English

# Error: java.lang.IllegalArgumentException error with a new triangular feature

## Error Message

When a polygon feature with three vertices is added to a shapefile, the following error message may occur:

"java.lang.IllegalArgumentException"

However, the same code works if the feature to be added has four or more vertices.

## Cause

The polygon boundary is not closed. The boundary is closed automatically if the number of vertices is 4 or more.

## Solution or Workaround

Close a polygon boundary with these steps:

1. Set the number of points to number of vertices + 1.
2. Get x, y coordinates for each vertex and set them to the polygon at respective indices.
3. Set the first point again at index = "number of vertices".
`Code://get the verticescom.esri.mo.cs.geom.Point point1=getFirstPoint();com.esri.mo.cs.geom.Point point2=getSecondPoint();com.esri.mo.cs.geom.Point point3=getThirdPoint();//number of verticesint num = 3;//read shapefile and get shape fieldShpFile shpfile=new ShpFile("D:/temp/testpoly.shp");PolygonShp polyshp= (PolygonShp)shpfile.getField();polyshp.setNumParts(1);//set number of points as number+1polyshp.setNumPoints(0,num+1);polyshp.setXAt(0,0,point1.x);polyshp.setYAt(0,0,point1.y);polyshp.setXAt(0,1,point2.x);polyshp.setYAt(0,1,point2.y);polyshp.setXAt(0,2,point3.x);polyshp.setYAt(0,2,point3.y);//add first point at index=number of verticespolyshp.setXAt(0,3,point1.x);polyshp.setYAt(0,3,point1.y);`

`Note:The example code uses classes from MapObjects Java 1.0.1. For MapObjects Java 2.0, use corresponding classes from this version.`

Last Published: 5/5/2016

Article ID: 000005610

Software: MapObjects-Java 2.0.1, 2.0, 1.0.1, 1.0