The most intelligent Java IDE
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of Flex 4 in Action this week in the Flex forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Beginning Java
 
RSS feed
 
New topic
Author

Getting a ArrayIndexOutOfBoundsException.

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Messages: 37

Hello,

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.

Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 10005

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Messages: 37

Hello Henry

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?


Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 10005

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Messages: 37

Humm, its strange array2[] has the value zero. i tried to print its length in the testMethod(). Why is the value of the array zero?

Thanks
Aditya

Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 10005

Aditya Sirohi wrote:Why is the value of the array zero?


Same hint: Does all calls to the getShare() method produce the same value? Does one of them return zero? What is different about that call?

Another hint: What happens when not all of the resources used by getShare() are initialized yet?

Henry

This message was edited 1 time. Last update was at by Henry Wong


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Messages: 37


Hello Henry,

I have a potential problem here, the getShare() method printed value of k as zero and 512. Why is that so?

Thanks
Aditya

Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 10005


Read my hints again

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Prime
Bartender

Joined: Oct 27, 2005
Messages: 8824

Does it always print 512, or just the last time you call it?

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Messages: 37

Hello Rob/Henry,

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.


Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 10005



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?


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Messages: 37


hint: What happens when not all of the resources used by getShare() are initialized yet?


I guess array2[] is initialized to value to a value zero. But what is that resource, i really cant see it.
Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 10005

Aditya Sirohi wrote:
But what is that resource, i really cant see it.


Take a look at the getShare() method, and tell me, what variables, that are external to the method, that it depends on?


And BTW, I am going to ignore the sarcastic part of your answer.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Messages: 37

Henry asked?
Take a look at the getShare() method, and tell me, what variables, that are external to the method, that it depends on?


I think they are array1, platform, ga_build_number and path.
Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 10005

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?

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Messages: 37

Thanks Henry,

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

 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Beginning Java
 
RSS feed
 
New topic
JProfiler
Get rid of your performance problems and memory leaks!