• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Out of Bounds Exception

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)



 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Sudheer Kolanu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Hey Christophe Verré, then why do we write often

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To initialize two elements. Try printing all elements; you'll see that 2 through 9 are all still null.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic