aspose file tools*
The moose likes Beginning Java and the fly likes Unexpected Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unexpected Exception" Watch "Unexpected Exception" New topic
Author

Unexpected Exception

Vivek Alampally
Ranch Hand

Joined: Jul 10, 2008
Posts: 67
Kindly look at the following program & output , and kindly tell me why it is generating ArrayIndexOutOfBoundsException


Output:
java.lang.ArrayIndexOutOfBoundsException: 1
at TestMain.addName(TestMain.java:40)
at TestMain.getNames(TestMain.java:31)
at TestMain.go(TestMain.java:18)
at TestMain.main(TestMain.java:13)
[Vivek, Santosh, Kathik Reddy, Karthik, Sravan, Bhanu, Shailendra, Rakesh, Vivek]
[Bhanu, Karthik, Kathik Reddy, Rakesh, Santosh, Shailendra, Sravan, Vivek, Vivek]


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Is that line with ----- part of your file? Because the line.split will return an array with less than 3 elements if there aren't that many /s in the file.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vivek Alampally
Ranch Hand

Joined: Jul 10, 2008
Posts: 67
Are you talking about the line which is used to underline Names.txt? If yes, there is no such line in Names.txt file. Names.txt file starts with Vivek/02K01A0560/25...........
And, did you notice a thing about output? ( Output is absolutely right, but I am worried about exception ?)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Why don't you print out the line and the results from splitting?
This will show you where it's going wrong.
Vivek Alampally
Ranch Hand

Joined: Jul 10, 2008
Posts: 67
I noticed one thing here, when we replace third argument in names.txt file with a string then it is not giving any exception.
Vivek Alampally
Ranch Hand

Joined: Jul 10, 2008
Posts: 67
Sorry above statement worked only once. Now its not working. I mean it is still giving an exception.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

And what do these two printlns I suggested say?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unexpected Exception