Solved

Adding element to a Vector causes StackOverflowError

Posted on 2007-04-03
2
167 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 107
web application structure 18 100
Crystal Reports Licensing Questions 4 35
Java syntax, or is it Selenium 6 30
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

839 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