Solved

Adding element to a Vector causes StackOverflowError

Posted on 2007-04-03
2
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

707 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