Solved

Adding element to a Vector causes StackOverflowError

Posted on 2007-04-03
2
164 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
Comment Utility
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
Comment Utility
Line 25 - create a different object than AClass. Try with something like:

Integer x = new Ineteger(10);
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi2 challenge 7 43
Free Alternative to JIRA 4 49
JDeveloper 12c for 32 bit 4 33
eclipse formatting 6 38
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

763 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

9 Experts available now in Live!

Get 1:1 Help Now