File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Stock Overflow Error

Alex Mun
Greenhorn

Joined: Oct 05, 2004
Posts: 14
Hi,

I have the following code:

import java.io.*;

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

public PilaEnlazada()
{
n.elemento=0;
n.top=null;
n.fin=null;
n.aux=null;
}

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

temp.elemento=e;
if (n.top==null)
{
n.top=n;
n.aux=n;
}
if (n.top!=null)
{
temp.elemento=e;
temp.fin=top;
top=temp;
}
}

public static void main(String args[])
{
PilaEnlazada nuevo=new PilaEnlazada();
nuevo.push(1);
}
}

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>(PilaEnlazada.java:7)

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

Any ideas?

Thanks.
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.
ex:

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.

Solution:
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
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html

but it extends Vector which could cause performance issues with synchronization
 
 
subject: Stock Overflow Error