Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Which is out of bounds?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Which is out of bounds?

    int m, mc;
    	Foundations give = new Foundations();
    	LinkedList<String>[] maneuver = new LinkedList[m];
    	LinkedList<String> distribute(){
    		m = 7;
    		for (int i = 0; i != m; i++){
    			maneuver[i] = new LinkedList<String>();
    			System.out.println ("maneuver[" + i + "]");
    		}
    		return maneuver[m];
    	}
    	//give.receive();
    }

    I get this error: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0"

    I have previously made a code similar to this but I did not get the same error. What could be wrong this time?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Which is out of bounds?

    You tell us. You have to step through this with a debugger, or at least add some print statements, to figure out what's going on. Recommended reading: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Which is out of bounds?

    Also, if you want help, please provide an SSCCE that demonstrates what you're doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Which is out of bounds?

    Quote Originally Posted by titowinky View Post
    int m, mc;
    	Foundations give = new Foundations();
    	LinkedList<String>[] maneuver = new LinkedList[m];
    	LinkedList<String> distribute(){
    		m = 7;
    You're defining "m" after using it as "maneuver"s size.

    Try this:

    int m = 7, mc;
    	Foundations give = new Foundations();
    	LinkedList<String>[] maneuver = new LinkedList[m];
    	LinkedList<String> distribute(){

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Which is out of bounds?

    Thank you Tim. I figured that out yesterday after a colleague pointed it to me. I should have declared the value first before using it.
    As a Java programming enthusiast, I was expecting people to help me in this forum because I can't figure it out myself. If I could, I would have not ask anybody. But each time I ask question here people will reply: if you want help do this first, read this, etc. I would have wanted to but I don't have the luxury of time. Of course you learn from books but it's easier to learn from some experienced guy.
    Thanks again Tim.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Which is out of bounds?

    Quote Originally Posted by titowinky View Post
    Thank you Tim. I figured that out yesterday after a colleague pointed it to me. I should have declared the value first before using it.
    As a Java programming enthusiast, I was expecting people to help me in this forum because I can't figure it out myself. If I could, I would have not ask anybody. But each time I ask question here people will reply: if you want help do this first, read this, etc. I would have wanted to but I don't have the luxury of time. Of course you learn from books but it's easier to learn from some experienced guy.
    Thanks again Tim.
    We ask you for an SSCCE and to do some basic debugging for several reasons. First off, many questions here are homework, so just giving out answers is just like doing somebody's homework for them. It teaches them nothing, and will actually end up hurting them for the reasons listed here: The Problem with Spoon-feeding

    Secondly, and more applicable in this situation, the code that people post is often NOT indicative of their actual problem. Tim had a good guess this time around, but I should point out that if we ran the code that you actually posted, it would result in a completely different compiler error. So either that code is different from the code you're running (in which case all bets are off) or you're incorrect about the compiler error (in which case you need to take a closer look at the code and error message). Both of these problems are solved by asking you to do some basic debugging and posting an SSCCE for us to look at. If you can't take the 5 minutes to do that, you can't really be that indignant that people aren't helping you, for free, in their spare time, just because they don't automatically do it for you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Array Index Out of bounds?
    By blazeking in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2012, 02:44 AM
  2. Can't Fix Array Out of Bounds
    By Apocalypse in forum Collections and Generics
    Replies: 5
    Last Post: October 31st, 2011, 08:37 AM
  3. Array exception out of bounds
    By gabberlt in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 17th, 2011, 09:05 AM
  4. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 07:11 AM
  5. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 12:19 PM