• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

ArrayIndexOutOfBoundsException

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am receiving an Array Out of Bounds Exception when trying to read the Instructors from a file.

Here are my errors:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at controller.CourseFileHelper.getInstructor(CourseFileHelper.java:72)
at controller.CourseFileHelper.readFile(CourseFileHelper.java:40)
at tests.CourseFileHelperTest.main(CourseFileHelperTest.java:79)

Line 79 CourseFileHelperTest

Line 72 CourseFileHelper

Line 40 CourseFileHelper


Courses.txt:
12345|BIO101|11:15|12:55|Wanda Jean Smith wsmith@dmacc.edu|Building : 3 Room Number: 17 Campus: Ankeny Type: Science Lab Capacity: 22
23456|CIS171|10:10|12:20|Wilma Fitz wfitz@dmacc.edu|Building : 8 Room Number: 17 Campus: Ankeny Type: Computer Lab Capacity: 25
29876|LIT105|15:15|16:30|Emily Theo etheo@dmacc.edu|Building : 2 Room Number: 12 Campus: Urban Type: Classroom Capacity: 18

Instructors.txt:
Wanda Jean,Smith,null
Wilma,Fitz,null
Emily,Theo,null
 
Marshal
Posts: 66575
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you inspected the array anywhere to confirm its length? How are you converting those lines to arrays? If you are using String#split(), what arguments are you passing to that method? Why have you got that cast in the first line of code (line 79)?
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Have you inspected the array anywhere to confirm its length? How are you converting those lines to arrays? If you are using String#split(), what arguments are you passing to that method? Why have you got that cast in the first line of code (line 79)?


I inspected it and the, array is supposed to be 3 parts because that is what the instructor creates. The program sort of works if I change the index of the array to 0 so part[0],part[0],part[0] but then I recieve the same error just with the classroom file instead of the instructor. I am using String[] parts = string.split("[\\[=,\\]]"); and only arguments being passed in is a string that gets split; private Instructor getInstructor(String string)
 
Campbell Ritchie
Marshal
Posts: 66575
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan Montgomery wrote:. . . array is supposed to be 3 parts . . .

In which case you ccan use indices 0 1 2. Have you printed the length of the array to verify it is length 3?

change the index of the array to 0 so part[0],part[0],part[0] . . .

No, that won't work, will it?
Where does the =, part of your regex come from? I can't see any = or commas in the file sample you showed.

[edit]Spelling corrections
 
Master Rancher
Posts: 4371
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thing to do is print out what is being passed into the getInstructors method.
You can then at least post that here if it looks wrong (or right, for that matter).

Then you need to print out the value of parts inside that method (I assume that the parts inside getInstructor is not the same as the parts in the readFile method).
Use Arrays.toString for that, otherwise you'll just get a hashcode.

Again, post that here, if you don't spot the problem.
 
Campbell Ritchie
Marshal
Posts: 66575
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . . Use Arrays.toString for that, otherwise you'll just get a hashcode. . . .

Or use JShell, which prints the array directly (large arrays are abbreviated).
Again, post that here, if you don't spot the problem
 
Saloon Keeper
Posts: 6462
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am re-posting my previous thread as I have still been unable to figure out how to fix the errors.
Here are the errors:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at controller.CourseFileHelper.getInstructor(CourseFileHelper.java:72)
at controller.CourseFileHelper.readFile(CourseFileHelper.java:40)
at tests.CourseFileHelperTest.main(CourseFileHelperTest.java:79)

Line 72


Line 40


Line 79


courses.txt is formatted as such:
12345|BIO101|11:15|12:55|Wanda Jean Smith wsmith@dmacc.edu|Building : 3 Room Number: 17 Campus: Ankeny Type: Science Lab Capacity: 22
23456|CIS171|10:10|12:20|Wilma Fitz wfitz@dmacc.edu|Building : 8 Room Number: 17 Campus: Ankeny Type: Computer Lab Capacity: 25
29876|LIT105|15:15|16:30|Emily Theo etheo@dmacc.edu|Building : 2 Room Number: 12 Campus: Urban Type: Classroom Capacity: 18
instructors.txt:
Wanda Jean,Smith,null
Wilma,Fitz,null
Emily,Theo,null
 
Carey Brown
Saloon Keeper
Posts: 6462
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Nathan Montgomery wrote:. . . array is supposed to be 3 parts . . .

In which case you ccan use indices 0 1 2. Have you printed the length of the array to verify it is length 3?

change the index of the array to 0 so part[0],part[0],part[0] . . .

No, that won't work, will it?
Where does the =, part of your regex come from? I can't see any = or commas in the file sample you showed.


I see to attempt on your part to respond to this.
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Campbell Ritchie wrote:

Nathan Montgomery wrote:. . . array is supposed to be 3 parts . . .

In which case you ccan use indices 0 1 2. Have you printed the length of the array to verify it is length 3?

change the index of the array to 0 so part[0],part[0],part[0] . . .

No, that won't work, will it?
Where does the =, part of your regex come from? I can't see any = or commas in the file sample you showed.


I see to attempt on your part to respond to this.


I tried using indices 0,1,2 but that did not work and I only recieved the same errors. I have not printed the length of the array, but I will go ahead and try that. The = sign seems to not be apart of any of the files from what I now realize? The commas however, are apart of the Instructors.txt file as such:
Wanda Jean,Smith,null
Wilma,Fitz,null
Emily,Theo,null
 
Carey Brown
Saloon Keeper
Posts: 6462
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the code where you are reading, parsing, and processing, "instructors.txt".  We're being asked to guess how your code was written based on your description. (Did you post it elsewhere and not tell us?)
 
Dave Tolls
Master Rancher
Posts: 4371
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this:

is line 72 then there is nothing in that line that would throw an ArrayIndexOutOfBoundsException.
Consequently that's not the line throwing the exception, or the code you posted is not the code being executed.
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:If this:

is line 72 then there is nothing in that line that would throw an ArrayIndexOutOfBoundsException.
Consequently that's not the line throwing the exception, or the code you posted is not the code being executed.


Apologies, line 72 is this:


And here is where instructors.txt is being created:
 
Dave Tolls
Master Rancher
Posts: 4371
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so we're back at this point.

What is parts at that point, and what data does it hold?
Add some debug printlns there (you'll need to use Arrays.toString() to print out the array).

I assume this is from the parameter passed in as parts[3] earlier (on line 40)?
So I would suggest printing out that parts array as well.

Edit: At the moment you are flying blind, and encountering an error and deciding you have no idea why.  And that's because you aren't printing out any useful debug information that you can use to figure it out.  Or at least to show us so we can help point you in the right direction.
 
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the first thing to check is that parts is actually as long as you think it is. If you’re accessing parts[4], then make sure that parts.length is at least 5.
 
Nathan Montgomery
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:OK, so we're back at this point.

What is parts at that point, and what data does it hold?
Add some debug printlns there (you'll need to use Arrays.toString() to print out the array).

I assume this is from the parameter passed in as parts[3] earlier (on line 40)?
So I would suggest printing out that parts array as well.

Edit: At the moment you are flying blind, and encountering an error and deciding you have no idea why.  And that's because you aren't printing out any useful debug information that you can use to figure it out.  Or at least to show us so we can help point you in the right direction.


Fixed it. I have yet to understand why, but when I changed it to be part[0],part[0],part[0] everything displayed correctly. I am confused to as why because the instructions I was given say the information is in the first 3 parts but this will have to do for now thanks.
 
Dave Tolls
Master Rancher
Posts: 4371
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that's because you are flying blind.
Until you start debugging properly you'll continue to throw in fixes that may or may not actually be fixes.
 
Campbell Ritchie
Marshal
Posts: 66575
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan Montgomery wrote:. . . when I changed it to be part[0],part[0],part[0] everything displayed correctly. . . .

As DT has told you, that isn't a solution. I think there are other errors somewhere, which more by luck than good management, you have managed to reverse with your latest error. It will count as an error because you aren't following the instructions you were given.
 
Carey Brown
Saloon Keeper
Posts: 6462
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't understand the reluctance to instrument your code. Here is how you'd do it.
 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!