Results 1 to 12 of 12
- 02-22-2011, 09:44 AM #1
Member
- 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):
I really can't figure out how to solve this. I've tried a hundred different aproaches. Would really appriciate som help.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(counter-1); 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(counter-1); 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); } }
- 02-22-2011, 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
PS. Forum it is not debug tool. You need to ask special questions.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
- 02-22-2011, 10:11 AM #3
Member
- 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...
- 02-22-2011, 10:43 AM #4
- 02-22-2011, 10:47 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
I don't understand what you mean?Where Is found this line 13 in your code?
The error at line 53:
is:Java Code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13 at NewProj.main(NewProj.java:53)
Java Code:newCoordinates[newLineCounter] = newCoor;
- 02-22-2011, 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
- 02-22-2011, 12:16 PM #7
Member
- 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(counter-1); 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(counter-1); 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); } }
- 02-22-2011, 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
- 02-22-2011, 12:39 PM #9
Member
- 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
- 02-22-2011, 01:09 PM #10
The piece of code above is a bit weird. See comments... The loop tries to iterate 15 times, inside which your if statement always increases the newLineCounter twice. And later on you are trying to access the array index based on the same newLineCounter.Java 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,
Your newLineCounter is exceeding that limit. Try to put some print statements on your own to see what's exactly happening.Java Code:Coordinate[] newCoordinates = new Coordinate[newLineLength]; [COLOR="Magenta"]//newLineLength = 13[/COLOR]
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-22-2011, 03:55 PM #11
Member
- 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(counter-1); 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(counter-1); 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); } }
- 02-22-2011, 05:23 PM #12
Similar Threads
-
ArrayIndexOutOfBoundsException: 0
By mxsar in forum New To JavaReplies: 3Last Post: 11-16-2010, 10:59 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
ArrayIndexOutOfBoundsException
By daredavil82 in forum New To JavaReplies: 2Last Post: 12-14-2007, 09:29 PM -
Error: java.lang.ArrayIndexOutOfBoundsException
By fernando in forum Java 2DReplies: 1Last Post: 07-31-2007, 11:47 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks