Results 1 to 12 of 12
  1. #1
    ArticRoot is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default 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)
    I really can't figure out how to solve this. I've tried a hundred different aproaches. Would really appriciate som help.

    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);
        }
    }

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    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
    PS. Forum it is not debug tool. You need to ask special questions.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    ArticRoot is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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...

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    Where Is found this line 13 in your code?
    Skype: petrarsentev
    http://TrackStudio.com

  5. #5
    ArticRoot is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Where Is found this line 13 in your code?
    I don't understand what you mean?

    The error at line 53:
    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
    at NewProj.main(NewProj.java:53)
    is:
    Java Code:
    newCoordinates[newLineCounter] = newCoor;

  6. #6
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    Well it is very difficult find mistake in your code. Can you post full code, which I can run.
    Skype: petrarsentev
    http://TrackStudio.com

  7. #7
    ArticRoot is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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);
        }
    }

  8. #8
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    It is not full code. What is that Coordinate, LineString, Point. Where Can i find this classes?
    Skype: petrarsentev
    http://TrackStudio.com

  9. #9
    ArticRoot is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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

  10. #10
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    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++;
    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.

    Your original array is defined for 13 elements max,

    Java Code:
    Coordinate[] newCoordinates = new Coordinate[newLineLength]; [COLOR="Magenta"]//newLineLength = 13[/COLOR]
    Your newLineCounter is exceeding that limit. Try to put some print statements on your own to see what's exactly happening.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  11. #11
    ArticRoot is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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);
    
        }
    }

  12. #12
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Good to know that you reached to the solution. Cheers :)

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. ArrayIndexOutOfBoundsException: 0
    By mxsar in forum New To Java
    Replies: 3
    Last Post: 11-16-2010, 10:59 PM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. ArrayIndexOutOfBoundsException
    By daredavil82 in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 09:29 PM
  4. Replies: 1
    Last Post: 07-31-2007, 11:47 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •