• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBoundsException on method  RSS feed

 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep getting ArrayIndexOutOfBoundsException when ever I try to call for the method below but I can't find anything wrong with it.


Thank you.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll find that your array has zero rows and zero columns, contrary to what you think. (Put in some debugging output which shows you that sort of thing.) Have a look at the order in which variables are initialized in your Mall class.
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are right I made it work by setting the width and length to a number but how is it not working when width and length are clearly set by the constructor?
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variables named "width" and "length" are assigned values by the constructor, that's true. But the array assigned to your "grid" variable is constructed before that happens. I suggested you review the order in which initialization of an object takes place, so here's how it works (in your case, anyway, it's a bit more complicated than this):

1. Instance variables are assigned values, if there is an assignment present. Otherwise they are assigned default values, which are always a suitable kind of zero.

2. The constructor runs, and it may assign different values to instance variables.

Do you see why your array has zero rows and columns now?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using Strings? Why don't you have a Shop class?
you should always use this format as a first attempt for all iterations of an array if you want to assign to the array. This format visits every element of the array and cannot throw an out of bounds Exception:-
for (int i = 0; i < myArray.length; i++) ...
In some circumstances you will have to change that; for example you might need to use myArray[i + 1] inside the loop, in which case you might suffer an out of bounds exception.
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answers, I solved the problem but 1 of my methods is giving me an ArrayIndexOutOfBoundsException:-1

Why when the customer at location 0,0 the method still accepts the 2 bottom ifs, even though its lower than 0? Is there something wrong with my code?
Thank you
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch!
Using == true is only one of the problems with that code.
Never use == true or == false which are poor style and also error‑prone.
Not if (b == true) ... or if (b == false) ...
Use if (b) ... or if (!b) ...

The reason the bottom ifs are being called is that their conditions are fulfilled. You have poorly‑formatted code without any spacing (look here for some suggestions) and the lines are too long, so they are difficult to read. You also have some confusing conditions. There are instances of −1 appearing (which you should write as − 1), as well as = combined with < or >. All those things make it much harder to understand what your conditions are. Always try to use if (b) ... rather than if (!b) ... if possible. Simply because it is easier to read. Similarly if you can use > rather than >= and < in preference to <= your code will be easier to understand. So your −1 and your <= will cause the last if to be called even when you have a 0 because that is the condition which is actually true.

I think you should not use numbers to represent those directions. Create yourself an enumerated type for directionsYou can probably encode the movement into the enum constants which is a nice object‑oriented way to do things.
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer I solved the problem, i am just using == true because that is what the method returns.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Nasr wrote:i am just using == true because that is what the method returns.

Then why stop at one?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!