Hi I"m receiving this on my program. I know that this happens when i choose a location outside of the map, but how do i fix this?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at GameMap.getLocation(GameMap.java:56)
return map[xdimension][ydimension];
at Game.startGame(Game.java:87)
System.out.println("New Location:" + mapMgr.getLocation(X,Y).getLocation() + ", "+ mapMgr.getLocation(X,Y).getDescription());
at Game.main(Game.java:168)
g.startGame();

how do i fix this?

Test the value of the indexes and make sure it is in bounds BEFORE using it to index into an array. You could use an if statement to test the indexes' values and skip using them if they are out of range.
Or check your logic to see why it creates an invalid index value and change that logic to keep the values in bounds.

All that are red are what java stated as Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

if(commandNum>5)
			{
				System.out.println("You have not Enter a valid command from 1 to 5. You must restart the Journey");
				System.out.println("Would You Like To Try Again? YES(1) NO(2)");
		    	playAgain=scan.nextInt();
		    	if (playAgain==1)
		    	{
		    	
	}
	private void move(int dx,int dy)
	{
	if ((X+dx<=0&&X+dx>mapMgr.getx()&&Y+dy<=0&&Y+dy>mapMgr.gety()))
			X+=dx;
			Y+=dy;
	}
	public static void main(String[] args) 
	{
				Game g = new Game("Baileym5_map.txt");
				g.startGame(); 
				g.printMap();
		
  	}	
	
}

Also with this code which is from another class

at GameMap.getLocation(GameMap.java:56)
return map[xdimension][ydimension];

So what you're saying is that i should use an if statement like if(y<0||x<0) System.out.println("Wrong Move")?

Something like that.
Which variable's value is < 0?

Please edit your post and wrap the code in code tags. Use the [code] icon above the input box.

Done.
X & Y would be less then zero if the user decided to move up or left on the first turn. for example if the use chooses to move up on the game map x would be negative. I have no clue where to put it in my Game class

where to put it.

It depends on what are you looking to add to the code?

I am trying to find out what to do when it goes out of bounds. How do i tell java to restart the game when the user goes out of bounds?

You should handle all error conditions by writing if statements to catch the invalid indexes, issue error messages as appropriate,ignore the bad input and have the program continue.

To exit the program, use: System.exit(0);

Yes thank you but I know how to exit the program, I just tried this
if (y<0||x<0) System.out.println("Wrong Move")
else startGame():

and it won't work it is still print out the same thing

You'll have to post the code.

You need to test for valid x,y values when the variables get values assigned to them. Look at where you set the values of x and y and think about where you can test their values as soon as possible after they are changed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.