• Post Reply Bookmark Topic Watch Topic
  • New Topic

2d array index out of bounds error  RSS feed

 
Wayne Harv
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recently had an assignment that I couldn't quite figure out. Below is the class that is gave me trouble



I have added a few printlns in the methods above to verify that I appear to be populating the array with the data I expected. In this case, it should be "?.?." for each iteration. The problem I am running into is that when I call assignCustomerAt it throws java.lang.ArrayIndexOutOfBoundsException: 1. I cannot figure out what is causing it, and I am not sure that I have constructed my methods correctly; can anyone shed some light on what I am doing wrong?

Thanks
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

First check in which method exception is being thrown. Then add print statements (line before code where trying to access array elements) to print "indices" as well as "array" sizes.
 
Wayne Harv
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I did as you suggested, and after initializing the array I see where it correctly reports the correct size as well as the indices. For example, I set it to [3][3] with ?.?. for each location, and I got the following just before the error occurs

3
?.?.
?.?.
?.?.
?.?.
?.?.
?.?.
?.?.
?.?.
?.?.

 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is assignCustomerAt being called? It looks like it's being called 9 times. Is it just a coincidence that that is the number of elements in the array?
 
Wayne Harv
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had it set to print out each instance, it wasnt actually being called 9 times. After digging further, I actually found there was a problem with the file that data was being read from. There was white space that was causing the first two objects to get set to null. Thanks guys for your help
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch too.
 
Nitin Kaushik
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I agree. Here's the link: https://www.hub4tech.com/java-tutorial
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid that tutorial is not helpful in the current circumstances.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!