File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Stock Overflow Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stock Overflow Error" Watch "Stock Overflow Error" New topic

Stock Overflow Error

Alex Mun

Joined: Oct 05, 2004
Posts: 14

I have the following code:


class PilaEnlazada
private int elemento;
private PilaEnlazada top,aux,fin;
PilaEnlazada n=new PilaEnlazada();

public PilaEnlazada()

public void push(int e)
PilaEnlazada temp=new PilaEnlazada();

if (
if (!=null)

public static void main(String args[])
PilaEnlazada nuevo=new PilaEnlazada();

When I compile this lines there is no error. But when I run it I obtain the following error:

java.lang.StackOverflowError at PilaEnlazada<init>(

I don't understand why it appears this error because I only have one number and any bucle.

Any ideas?

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Stack overflows are often due to recursion gone wild, so I looked for anything that referes to itself. We usually see a method call itself, but you found an interesting way to do it with object references. Look at your member variable definition:

PilaEnlazada n=new PilaEnlazada();

When your main() method creates a new instance of PilaEnlazada, this variable is initialized, and it creates another instance. And that new one creates another instance. As my wife always says about children's dirty laundry, it just never ends.

Describe your objective to us a bit in plain language (not code) and see if a better way to do it in code comes to mind.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
Corrected code:

Problems found:
The class variable 'n' and it's initialization

From the main when
PilaEnlazada nuevo=new PilaEnlazada();
is executed a recursion is started.

Below I will put the path of the recursion...

step 1. When new PilaEnlazada() is executed the class PilaEnzalada is loaded to memmory. If already loadecd ignore this step.
Step 2. JVM allots memory for all class variables.
Step 3. All class variables including 'n' will be initialized before executing the statements in constructor.
Step 4. As part of intializing n, the statement new PilaEnlazada() will be executed
Step 5. As part of step 4 above Steps 1 to 5 will be executed again.

Solution to the program:
(solution to the program is different from solution to the problem, I will explain the problem later)Remove recursion caused by class variable 'n'. Remove class variable 'n' and all it's references. The 'current' PilaEnlazada can be refrenced by 'this'. On the left hand side of statements, 'this' is implicit even if you don't specify them.

On the right hand side of statements, 'this' is explicitly needed.

Solution to the problem:
Solution is specific to the goal.
Goal identification:
What the programmer trying to accomplish here is either to establish a linked list of 'PilaEnlazada's or a collection of 'PilaEnlazada's.
Since PilaEnlazada itself have a behaviour called push it is assumed that the programmer is trying to impliment a 'stack kind of collection' for 'PilaEnlazada's. If the goal is different from assumed please let me know.

Needs atleast two types need to be defined. One type is PilaEnlazada. Another type is a 'stack kind of collection' which holds PilaEnlazada.
The code for PilaEnlazada may look like.

The code for PilaEnlazadaStack may look like follows

PilaEnlazadaHolder can be made as an inner class to PilaEnlazadaStack

Another solution is to use the Stack

but it extends Vector which could cause performance issues with synchronization
I agree. Here's the link:
subject: Stock Overflow Error
It's not a secret anymore!