#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    62
    Rep Power
    2

    ArrayIndexOutOfBoundsException error


    map01


    Code:
        00000
        10000
        22222



    Code:
        public void levelWCollision(){
                /*** FIND PLAYER POSTION ***/
            		int y = 0;
            		int x = 0;
            		for(y = 0; y < map01.length; y++){
            			for( x = 0; x < map01[y].length; x++){
            				if(map01[y][x] == 1){
            					break;
            				}
            			}
            		}
            		
            		//player is standing ontop of ground
            		if(map01[y+1][x] == 2){
            			System.out.println("WORKS");
            		}
        
        }


    but getting a error on line 114: "if(map01[y+1][x] == 2){"

    full error:
    Code:
    Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 14
    	at Level.levelWCollision(Level.java:114)
    	at Main.actionPerformed(Main.java:123)
    	at javax.swing.Timer.fireActionPerformed(Unknown Source)
    	at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    162
    Rep Power
    18
    After the for-loop, the value of 'y' would be equal to length of the array. As such there would be no element at an index position equal to length of array. So obviously, when you say 'map01[y+1]', it throws the IndexOutOfBounds exception.

IMN logo majestic logo threadwatch logo seochat tools logo