Solved

Adding element to a Vector causes StackOverflowError

Posted on 2007-04-03
2
165 Views
Last Modified: 2013-11-23
Hi,
I get a 'Stack overflow error:null' with the below code (relevant bits posted below) when adding an instance of class AClass

to a Vector. Does anyone know what's causing this, please help...

java.lang.StackOverflowError
      at AClass.addTank(AClass.java:25)
      at AClass.<init>(AClass.java:19)
      at AClass.addTank(AClass.java:25)
      at AClass.<init>(AClass.java:19)
      at AClass.addTank(AClass.java:25)
      at AClass.<init>(AClass.java:19)

Tonnes more of these

import java.util.Vector;

   public static Vector AClasss = new Vector();

    public AClass()
    {
        setX(targetX);
        setY(targetY);
        addTank();      //Line 19
        start();
    }


    public void addTank()
    {
        AClass AClass = new AClass();      //Line 25
        AClasss.addElement(AClass);         //Add the new instance to the vector
    }
0
Comment
Question by:mark_667
2 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 125 total points
ID: 18846834
The way you have it now, addTank() creates a new AClass object, but the constructor for AClass calls addTank(), which traps it in an endless loop until the Java VM runs out of space and gives up. Either you need to take the addTank() call out of the constructor, or the "new AClass()" line out of addTank().
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18848344
Line 25 - create a different object than AClass. Try with something like:

Integer x = new Ineteger(10);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now