Results 1 to 12 of 12
 02222011, 09:44 AM #1Member
 Join Date
 Feb 2011
 Posts
 9
 Rep Power
 0
Error: ArrayIndexOutOfBoundsException. Where is the problem?
Hi
I get the following error when running my program (JTS):
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at NewProj.main(NewProj.java:53)
Java Code:import com.vividsolutions.jts.geom.*; public class NewProj { public static void main(String[] args){ Coordinate[] coordinatesToLine = new Coordinate[] { new Coordinate(12, 2), new Coordinate(12, 13), new Coordinate(12, 19), new Coordinate(19, 19), new Coordinate(20, 20), new Coordinate(25, 25), new Coordinate(27, 18) }; LineString FirstLine = new GeometryFactory().createLineString(coordinatesToLine); Point pointOne, pointTwo; double dist; int newLineLength = FirstLine.getNumPoints(); double newCoorX, newCoorY; Coordinate newCoor; int points2Add; for (int counter=1; counter <= FirstLine.getNumPoints()1;counter++){ pointOne = FirstLine.getPointN(counter1); pointTwo = FirstLine.getPointN(counter); dist = pointOne.distance(pointTwo); newLineLength = newLineLength + (int)(dist / 5); } Coordinate[] newCoordinates = new Coordinate[newLineLength]; newCoordinates[0] = FirstLine.getCoordinateN(0); int newLineCounter=1; for (int counter=1; counter <= FirstLine.getNumPoints()1;counter++){ pointOne = FirstLine.getPointN(counter1); pointTwo = FirstLine.getPointN(counter); dist = pointOne.distance(pointTwo); newLineLength = newLineLength + (int)(dist / 5); for (int x=1; x <= newLineLength; x++){ dist = pointOne.distance(pointTwo); points2Add = (int)(dist / 5); if ( points2Add > 0 ){ for ( int i=1 ; i <= points2Add ; i++ ){ newCoorX = pointOne.getX() + ((double)i / ( (double)points2Add + 1)) * (pointTwo.getX()  pointOne.getX()); newCoorY = pointOne.getY() + ((double)i / ( (double)points2Add + 1)) * (pointTwo.getY()  pointOne.getY()); newCoor = new Coordinate(Math.round(newCoorX), Math.round(newCoorY)); newCoordinates[newLineCounter] = newCoor; newLineCounter++; } } } newCoordinates[newLineCounter] = FirstLine.getCoordinateN(counter); newLineCounter++; } LineString SecondLine = new GeometryFactory().createLineString(newCoordinates); System.out.println("oldline " + FirstLine); System.out.println("newline " + SecondLine); } }
 02222011, 10:06 AM #2
This is exception to say that you out of bounds in arrays.
for example you tried get element with index the more then size array.
for example
Java Code:String[] mass = new String[3]; //do something with array String value = mass[4]; // thwor example java.lang.ArrayIndexOutOfBoundsException
Skype: petrarsentev
http://TrackStudio.com
 02222011, 10:11 AM #3Member
 Join Date
 Feb 2011
 Posts
 9
 Rep Power
 0
Thank's for your reply Petr.
I understand the meaning of the exception, but I don't see what in my code that is wrong...
 02222011, 10:43 AM #4
 02222011, 10:47 AM #5Member
 Join Date
 Feb 2011
 Posts
 9
 Rep Power
 0
Where Is found this line 13 in your code?
The error at line 53:
Java Code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13 at NewProj.main(NewProj.java:53)
Java Code:newCoordinates[newLineCounter] = newCoor;
 02222011, 11:16 AM #6
Well it is very difficult find mistake in your code. Can you post full code, which I can run.
Skype: petrarsentev
http://TrackStudio.com
 02222011, 12:16 PM #7Member
 Join Date
 Feb 2011
 Posts
 9
 Rep Power
 0
Yeah, I know:)
Full code:
Java Code:import com.vividsolutions.jts.geom.*; public class NewProj { public static void main(String[] args){ Coordinate[] coordinatesToLine = new Coordinate[] { new Coordinate(12, 2), new Coordinate(12, 13), new Coordinate(12, 19), new Coordinate(19, 19), new Coordinate(20, 20), new Coordinate(25, 25), new Coordinate(27, 18) }; LineString FirstLine = new GeometryFactory().createLineString(coordinatesToLine); Point pointOne, pointTwo; double dist; int newLineLength = FirstLine.getNumPoints(); double newCoorX, newCoorY; Coordinate newCoor; int points2Add; for (int counter=1; counter <= FirstLine.getNumPoints()1;counter++){ pointOne = FirstLine.getPointN(counter1); pointTwo = FirstLine.getPointN(counter); dist = pointOne.distance(pointTwo); newLineLength = newLineLength + (int)(dist / 5); } Coordinate[] newCoordinates = new Coordinate[newLineLength]; newCoordinates[0] = FirstLine.getCoordinateN(0); int newLineCounter=1; for (int counter=1; counter <= FirstLine.getNumPoints()1;counter++){ pointOne = FirstLine.getPointN(counter1); pointTwo = FirstLine.getPointN(counter); dist = pointOne.distance(pointTwo); newLineLength = newLineLength + (int)(dist / 5); for (int x=1; x <= newLineLength; x++){ dist = pointOne.distance(pointTwo); points2Add = (int)(dist / 5); if ( points2Add > 0 ){ for ( int i=1 ; i <= points2Add ; i++ ){ newCoorX = pointOne.getX() + ((double)i / ( (double)points2Add + 1)) * (pointTwo.getX()  pointOne.getX()); newCoorY = pointOne.getY() + ((double)i / ( (double)points2Add + 1)) * (pointTwo.getY()  pointOne.getY()); newCoor = new Coordinate(Math.round(newCoorX), Math.round(newCoorY)); newCoordinates[newLineCounter] = newCoor; newLineCounter++; } } } newCoordinates[newLineCounter] = FirstLine.getCoordinateN(counter); newLineCounter++; } LineString SecondLine = new GeometryFactory().createLineString(newCoordinates); System.out.println("oldline " + FirstLine); System.out.println("newline " + SecondLine); } }
 02222011, 12:31 PM #8
It is not full code. What is that Coordinate, LineString, Point. Where Can i find this classes?
Skype: petrarsentev
http://TrackStudio.com
 02222011, 12:39 PM #9Member
 Join Date
 Feb 2011
 Posts
 9
 Rep Power
 0
Sorry about that Petr.
I'm using Java Topology Suit (JTS). See these links for the classes:
Coordinate
Point
LineString
All geom* objects:
com.vividsolutions.jts.geom
 02222011, 01:09 PM #10Java Code:
for (int x=1; x <= newLineLength; x++){ [COLOR="Magenta"]//newLineLength = 15[/COLOR] dist = pointOne.distance(pointTwo); [COLOR="Magenta"]//dist = 11.0[/COLOR] points2Add = (int)(dist / 5); if ( points2Add > 0 ){ [COLOR="Magenta"]//points2Add = 2[/COLOR] ....... newLineCounter++;
Your original array is defined for 13 elements max,
Java Code:Coordinate[] newCoordinates = new Coordinate[newLineLength]; [COLOR="Magenta"]//newLineLength = 13[/COLOR]
GoldestJava Is A Funny Language... Really!
Click on * and add to member reputation, if you find their advices/solutions effective.
 02222011, 03:55 PM #11Member
 Join Date
 Feb 2011
 Posts
 9
 Rep Power
 0
Goldest & Petr: Thanks for your efforts. Really appriciate it.
If anyone, for any reason, is interested in the solution, here it goes:
Java Code:import com.vividsolutions.jts.geom.*; public class NewProj { public static void main(String[] args){ Coordinate[] coordinatesToLine = new Coordinate[] { new Coordinate(12, 2), new Coordinate(12, 13), new Coordinate(12, 19), new Coordinate(19, 19), new Coordinate(20, 20), new Coordinate(25, 25), new Coordinate(27, 18) }; LineString FirstLine = new GeometryFactory().createLineString(coordinatesToLine); Point pointOne, pointTwo; double dist; int newLineLength = FirstLine.getNumPoints(); double newCoorX, newCoorY; Coordinate newCoor; int points2Add; for (int counter=1; counter <= FirstLine.getNumPoints()1;counter++){ pointOne = FirstLine.getPointN(counter1); pointTwo = FirstLine.getPointN(counter); dist = pointOne.distance(pointTwo); newLineLength = newLineLength + (int)(dist / 5);//here new number of points are added } Coordinate[] newCoordinates = new Coordinate[newLineLength]; newCoordinates[0] = FirstLine.getCoordinateN(0); int newLineCounter=1; for (int counter=1 ; counter <= FirstLine.getNumPoints()1 ; counter++){ pointOne = FirstLine.getPointN(counter1); pointTwo = FirstLine.getPointN(counter); dist = pointOne.distance(pointTwo); points2Add = (int)(dist / 5); if ( points2Add > 0 ){ for ( int i=1 ; i <= points2Add ; i++ ){ newCoorX = pointOne.getX() + ((double)i / ( (double)points2Add + 1)) * (pointTwo.getX()  pointOne.getX()); newCoorY = pointOne.getY() + ((double)i / ( (double)points2Add + 1)) * (pointTwo.getY()  pointOne.getY()); newCoor = new Coordinate(Math.round(newCoorX), Math.round(newCoorY)); newCoordinates[newLineCounter] = newCoor; newLineCounter++; } } newCoordinates[newLineCounter] = FirstLine.getCoordinateN(counter); newLineCounter++; } LineString SecondLine = new GeometryFactory().createLineString(newCoordinates); System.out.println("oldline " + FirstLine); System.out.println("newline " + SecondLine); } }
 02222011, 05:23 PM #12
Similar Threads

ArrayIndexOutOfBoundsException: 0
By mxsar in forum New To JavaReplies: 3Last Post: 11162010, 10:59 PM 
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11202009, 07:58 PM 
ArrayIndexOutOfBoundsException
By daredavil82 in forum New To JavaReplies: 2Last Post: 12142007, 09:29 PM 
Error: java.lang.ArrayIndexOutOfBoundsException
By fernando in forum Java 2DReplies: 1Last Post: 07312007, 11:47 PM 
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07252007, 10:55 PM
Bookmarks