Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Out of Bounds Exception

 
Sudheer Kolanu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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)


 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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!
 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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: 217
  • Mark post as helpful
  • send pies
  • 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

 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To initialize two elements. Try printing all elements; you'll see that 2 through 9 are all still null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic