• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stack overflow error !  RSS feed

 
Ashish Maharaja Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this error in the following code. Please tell me why this happened and how to prevent it.


 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your MyArray class has a member that is a MyArray, which has a member that is a MyArray, which has a member that is a MyArray, which has a member that is a MyArray, which has a member that is a MyArray, which has a member that is a MyArray, which has a member that is a MyArray, which has a member that is a MyArray, which has a member that is a MyArray ...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that it's entirely legal to have a class having a reference to itself like that, and there are some situations that it's very useful. It's creating a new instance every time that's the problem.

So this is OK, because you can always pass null in:

But this tries to create an infinite number of objects as soon as you create the first one:
 
Ashish Maharaja Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for pointing the error.
Code works now.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!