wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Array Out of Bounds Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Out of Bounds Exception" Watch "Array Out of Bounds Exception" New topic
Author

Array Out of Bounds Exception

Sudheer Kolanu
Greenhorn

Joined: Nov 04, 2010
Posts: 13
Dear Friends,

I am trying this "Be the compiler" example from Head First Java, Chapter 3. The below is the code and output. I have multiple questions on this:

1. I did not understand the flow (Sorry I am new bee)
2. I am getting Array Out of Bounds Exception, what is causing for this and how would I rectify it?
3. Why the 'Z' was incremented right after the condition in while loop?

Precisely, I am asking you to help me understand this program. Appreciate for your time!




C:\Library\Headache\Pro>java Hobbits
frodo is a good Hobbit name
Sam is a good Hobbit name
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Hobbits.main(Hobbits.java:13)


Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Arrays index starts from 0. So you'll access elements from an array whose size is 3 via index 0,1,2. Now, try to think of the value of 'z' in the while loop. How is it growing ?


[My Blog]
All roads lead to JavaRanch
Sudheer Kolanu
Greenhorn

Joined: Nov 04, 2010
Posts: 13
Hello,

I moved the Z increment as the last statment. However I still get this Array Out of Bounds Exception. The below is the output:

C:\Library\Headache\Pro>java Hobbits
bilbo is a good Hobbit name
frodo is a good Hobbit name
Sam is a good Hobbit name
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Hobbits.main(Hobbits.java:13)
Sudheer Kolanu
Greenhorn

Joined: Nov 04, 2010
Posts: 13
Hello again,

I changed the condition to be Z<3 which solved the purpose. Since there are only three elements in array making it less than 3 would suffice. Thank you very much!
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
h[z]= new Hobbits();


Please tell me why we need this statement ? What we are exactly doing here ? Are we storing the object inside the array declared, if yes, then whats the need for that ?

Thanks


Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Vinod Vijay wrote:
h[z]= new Hobbits();


Please tell me why we need this statement ? What we are exactly doing here ? Are we storing the object inside the array declared, if yes, then whats the need for that ?

Thanks

Because even if you instantiate a new array, it won't magically instantiate its elements.
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
Because even if you instantiate a new array, it won't magically instantiate its elements.


Hey Christophe Verré, then why do we write often

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

To initialize two elements. Try printing all elements; you'll see that 2 through 9 are all still null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Out of Bounds Exception