Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array out of bounds error for unknown reason

 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting an ArrayOutOfBoundsException. My code:






I have left out the try/catch blocks in order to get the exception, but I don't understand why the error.
After the split, the parameters are for the delimiter and the size of the array, right? Is this not where I
declare the size of the array? I'm trying to input from a text file with the name of a photo, the photographer's
name, the file type, and a double for the file size. It is delimited by "|" and I want to create an array to hold
this information of four elements.
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marjorie Gyles wrote:I'm getting an ArrayOutOfBoundsException. My code:






I have left out the try/catch blocks in order to get the exception, but I don't understand why the error.
After the split, the parameters are for the delimiter and the size of the array, right? Is this not where I
declare the size of the array? I'm trying to input from a text file with the name of a photo, the photographer's
name, the file type, and a double for the file size. It is delimited by "|" and I want to create an array to hold
this information of four elements.


No... the second parameter is a limit on the split -- meaning after the split is done, the array will be at most 4 elements. If there aren't three delimiters, then it is possible to have less than four elements.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would always suggest you use System.out.println() to see what your code is doing. You can always take them out.

I would suggest you print the size of the array and see if it really is four elements each time. Due to the NPE, it must not be.
 
Jinu Vijay
Greenhorn
Posts: 20
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check the validity of the line being read from the file by an SOP. A sample code is below for reference.

String str = "photoName|photographerName|fileType|20.2";
String[] fields = str.split("\\|", 4);
System.out.println(fields[0] + " " + fields[1] + " " + fields[2] + " "
+ Double.parseDouble(fields[3]) + " ");

Output:
photoName photographerName fileType 20.2
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jinu Vijay welcome to the Ranch
 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting the error message all over my input when I add the System.out.print statement.
I'm not supposed to put a delimit symbol at the end of the lines am I? I thought that was what
the enter character was for. I tried the suggestion from Jinu and it worked perfectly, so at this
point I'm wondering what's wrong with my input file.


Error output:
My Kittens Joe Miller jpeg 1.4
Beauty Spot Marty Barrett jpeg 2.1
Funny Bob Farnsworth gif 0.5
Albino Possum Don Bailes png 3.9
Xylophone Alvin Chipmunk gif 0.8
Colorful Zombie Elliemae Clampett png 2.2
Barracuda Ben Dover jpeg 8.7
Fig Newton Willie Makit png 4.3
Sunset Betty Wont jpeg 6.1
4-eye Butterfly fish Claude Badly jpeg 9.2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at PhotoTracker.main(PhotoTracker.java:94)



Line causing the problem:




 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you share the input file contents you are trying to tokenize? I would like to see that.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to get the 4th element in a 3‑element array. Before that statement, print out the line you are splitting, and the whole array after splitting, using the Arrays#toString() method, and its length field.
 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:Could you share the input file contents you are trying to tokenize? I would like to see that.


The "error output" listing is the information I am trying to import.
"My Kittens Joe Miller jpeg 1.4" etc...
I don't understand why it's importing it, printing it, and still giving me an error message.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marjorie Gyles wrote: I don't understand why it's importing it, printing it, and still giving me an error message.

My guess is there is an error on the input - instead of getting 3 delimeters, you only get 2. You need to do what Campbell asked you to do.
 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You are trying to get the 4th element in a 3‑element array. Before that statement, print out the line you are splitting, and the whole array after splitting, using the Arrays#toString() method, and its length field.




Gets me:
My Kittens
Joe Miller
jpeg
1.4
Beauty Spot
Marty Barrett
jpeg
2.
Funny
Bob Farnsworth
gif
0.5
Albino Possum
Don Bailes
png
3.9
Xylophone
Alvin Chipmunk
gif
0.8
Colorful Zombie
Elliemae Clampett
png
2.2
Barracuda
Ben Dover
jpeg
8.7
Fig Newton
Willie Makit
png
4.3
Sunset
Betty Wont
jpeg
6.1
4-eye Butterfly fish
Claude Badly
jpeg
9.2
My Sheep
Bo Peep
12
>
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice there is a break in the pattern

My Sheep
Bo Peep
12
 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not have thought that would give an array out of bounds error. I would think more along the lines of a bad number format instead. I should be able to catch it and still write the rest of the text into the array, though, right? (I'm currently sitting in a classroom and am typing on my nook, so I can't play with the code at the present time.)
 
Winston Gutkowski
Bartender
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marjorie Gyles wrote:I would not have thought that would give an array out of bounds error.

Marjorie,

You still haven't supplied us with the input that caused the error. Until you do, we're just guessing.

Winston
 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Marjorie Gyles wrote:I would not have thought that would give an array out of bounds error.

Marjorie,

You still haven't supplied us with the input that caused the error. Until you do, we're just guessing.

Winston


Sorry, Winston, I thought I made it clear. The Input file...

My Kittens|Joe Miller|jpeg|1.4
Beauty Spot|Marty Barrett|jpeg|2.
Funny|Bob Farnsworth|gif|0.5
Albino Possum|Don Bailes|png|3.9
Xylophone|Alvin Chipmunk|gif|0.8
Colorful Zombie|Elliemae Clampett|png|2.2
Barracuda|Ben Dover|jpeg|8.7
Fig Newton|Willie Makit|png|4.3
Sunset|Betty Wont|jpeg|6.1
4-eye Butterfly fish|Claude Badly|jpeg|9.2
My Sheep|Bo Peep|12
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice on the last line, you only have 3 tokens and not 4. So the actual array is only going to contain 3 members not 4, so 3 is not a valid index.
 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith Lynn wrote:Notice on the last line, you only have 3 tokens and not 4. So the actual array is only going to contain 3 members not 4, so 3 is not a valid index.


After you pointed that out, I realized you were right. However, I don't know how to get around the bad input other than to catch it.
If I try and catch it, I won't be able to input the rest of the file. How do I ignore this particular input and input the rest of the file?
He never taught us that and I'm having a hard time finding the answer anywhere.
 
Jinu Vijay
Greenhorn
Posts: 20
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marjorie Gyles wrote:I'm getting the error message all over my input when I add the System.out.print statement.
I'm not supposed to put a delimit symbol at the end of the lines am I? I thought that was what
the enter character was for. I tried the suggestion from Jinu and it worked perfectly, so at this
point I'm wondering what's wrong with my input file.


Error output:
My Kittens Joe Miller jpeg 1.4
Beauty Spot Marty Barrett jpeg 2.1
Funny Bob Farnsworth gif 0.5
Albino Possum Don Bailes png 3.9
Xylophone Alvin Chipmunk gif 0.8
Colorful Zombie Elliemae Clampett png 2.2
Barracuda Ben Dover jpeg 8.7
Fig Newton Willie Makit png 4.3
Sunset Betty Wont jpeg 6.1
4-eye Butterfly fish Claude Badly jpeg 9.2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at PhotoTracker.main(PhotoTracker.java:94)



Line causing the problem:







Here ... after the below ...



you could first check the length of fields (i.e. fields.length) and if it is < 4 may be log the error, and continue the loop to read the next line.
 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jinu Vijay wrote:you could first check the length of fields (i.e. fields.length) and if it is < 4 may be log the error, and continue the loop to read the next line.


Thanks! That worked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic