• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




I get a NullPointerException. Why?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're creating a Circle array. But when it's initialised it doesn't contain any Circle objects - each entry is null.
 
vanio begic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is because you created a circle object with array like name respectivly being :
And way you shoulda done it was this :
You got the exception because you tried to acces non-existent array in array like way.Aka you tried to acces Circle[0] when in fact you didnt even have array of objects,instead what you had was ordinray object on the heap.

I hope this helps
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, both those lines of code are identical (though I personally think the second is better style) - that's not the cause of the exception. The cause is as I said - no Circle objects have been created. All you have is an array of null values. That's why it's a NullPointerException, not an ArrayIndexOutOfBoundsException.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I think I get it. I created 500 slots (0-499) that can contain objects, but didn't actually create any objects?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:Ah, I think I get it. I created 500 slots (0-499) that can contain objects, but didn't actually create any objects?

 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:Ah, I think I get it. I created 500 slots (0-499) that can contain objects, but didn't actually create any objects?


Exactly! Just because you created an egg carton, you haven't yet gotten the hens to lay some eggs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!