I am getting this error while compiling this class below. The error is:-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Path.getShareableEntFolder(Path.java:52)
at Path.main(Path.java:16)
When i try to change to it works fine. What might be the issue Help is appreciated.
Aditya Sirohi wrote:
I am getting this error while compiling this class below. The error is:-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Path.getShareableEntFolder(Path.java:52)
at Path.main(Path.java:16)
The stack trace is telling you most of the story. If you look, you will see that the exception is thrown when trying to access element zero. This means that there isn't an element zero, which states that the array is of size zero. You should probably confirm this by adding a line of debugging code.
Obviously, you didn't want your array to have no elements, so I would recommend to take another look at the initialization to see what is wrong.
Henry
This message was edited 1 time. Last update was at by Henry Wong
Obviously, you didn't want your array to have no elements, so I would recommend to take another look at the initialization to see what is wrong.
But when i try to print the value of "k" from getShare() method, i get a value of 512. which should be initialized when array is given a size:- static String array2[] = new String[getShare()];
Could you tell me more what i am doing wrong?
Aditya Sirohi wrote:
But when i try to print the value of "k" from getShare() method, i get a value of 512. which should be initialized when array is given a size:- static String array2[] = new String[getShare()];
Could you tell me more what i am doing wrong?
Hint: When did you "try to print the value of "k" from getShare() method? At the time that it is called? Or did you call it separately?
Another way to ask it. You call getShare() many times. Do they *all* return 512?
Henry
This message was edited 1 time. Last update was at by Henry Wong
I have faced this issue first time So i want some ideas and reason what wrong i am doing here. So in this class below getShare() method print value of XX as 512.
But when i change the code and try to call the getShare() method inside array2[] so that array2[] can be initialized to the value of XX i get from getShare(). But length for array2[] is zero where at it should be value of XX which is returned from getShare(), ie 512. What is the problem. I want array2[] to get the value of getShare(). I need to tweek something let me know.
Note: I am not just giving you hints because I want to make you jump through hoops. I know exactly what the problem is, and is trying to get you to see it. Please take a look at the hints. The answer is there. So....
hint: What happens when not all of the resources used by getShare() are initialized yet?
Aditya Sirohi wrote:
I think they are array1, platform, ga_build_number and path.
Now... next question... when are all four of those variables all initialized?
And since I am going to bed now... the next next question is... At the time the getShare() method is called to create the array2 array, did that [ANSWER_FROM_FIRST_QUESTION] get called yet?
You are a great guide . So it was my eyes that could not figure this out. So i called pushInArray(). Have a good night. Marking this String as resolved.
Thanks
Aditya
subject: Getting a ArrayIndexOutOfBoundsException.