C4d
C4d

Reputation: 3282

Subclass instantiation | Stackoverflowexception

I got a lot of code, but the problem itself is very clear to me so I'm posting an example:

public class ExcelTable
{
    public ExcelTable() // CONSTRUCTOR
    {
        // create new excel-application and so on..
    }

    public ExcelOutput Output = new ExcelOutput(); // Stackoverflowexception

    private xlWorkbook;  // I need these to inherit into ExcelOutput
    private xlWorksheet; // ''    ''
    // ...               // ''    ''

    public class ExcelOutput : ExcelTable
    {
        public void SaveAs()
        {
            // self explaining
        }

        public void Show()
        {
            // self explaining
        }

        public void Print()
        //....
    }
}

Goal: EDITED
In purpose of a clear helping-class for excel my wish is to create a subclass which takes care for the possible output. In detail I'm trying to prevent the need to create 2 instances from outside => ExcelTable and ExcelOutput. Im rather would like to create a single instance ExcelTable and access the subclass with this instance. Because I need the inheritance, I cant set the class to static.

Problem:
Im getting a Stackoverflowexception. I tried debugging my code. At the instantiation of ExcelOutputthe code is looping at this single line. Means: Even if I try to Single-Step (F11) it, the execution stays at the instatiation of ExcelOutput which leads my tool to the Stackoverflowexception.

What I've tried:

public ExcelTable() // CONSTRUCTOR
{
    // create new excel-application and so on..

    Output = Output ?? new ExcelOutput();
}

public ExcelOutput Output;

In this case, ExcelTable is created over and over again. This whole upper class gets instatiated for ever.

So could anyone explain to me why this single line is looping forever? It looks like the instantiaton of ExcelOutput is creating a new instance of ExcelTable.


I know this is more about knowledge as Im a newbie in case of inheritance and subclasses. Google wasnt a big help in here. There's just too much stuff out there according classes, subclasses and inheritance.

Every help is appreciated. Thanks a lot!

Upvotes: 0

Views: 208

Answers (2)

Yacoub Massad
Yacoub Massad

Reputation: 27861

As mentioned before, the problem is that ExcelOutput inherits from ExcelTable and thus inherits the Output field which is initialized with a new instance of ExcelOutput which again has an Output field ...

This recursive composition will fill the stack and thus cause the StackOverflowException exception.

Here is a quick fix that will get you going:

public class ExcelTable
{
    public ExcelTable() 
    {
        // create new excel-application and so on..
        //initialize xlWorkbook and xlWorksheet

        Output = new ExcelOutput(xlWorkbook, xlWorksheet);
    }

    public ExcelOutput Output;

    private Workbook xlWorkbook;
    private Worksheet xlWorksheet;

    public class ExcelOutput
    {
        private Workbook xlWorkbook;  
        private Worksheet xlWorksheet;

        public ExcelOutput(Workbook xl_workbook, Worksheet xl_worksheet)
        {
            xlWorkbook = xl_workbook;
            xlWorksheet = xl_worksheet;
        }

        public void SaveAs()
        {
            // self explaining
        }

        public void Show()
        {
            // self explaining
        }

        public void Print()
        {

        }
        //....
    }
}

ExcelOutput no longer inherits from ExcelTable, but it receives the required dependencies upon construction.

My solution is in no way a good design. I am just giving you a quick fix. A good design would require an understanding of the bigger picture.

Upvotes: 1

EduardoS
EduardoS

Reputation: 199

When you instantiate a ExcelTable or ExcelOutput the constructor will initialize the Output field, in other words, it will instantiate another ExcelOutput, and then again and again until you run out of stack space, then you receive this StackOverflowException, you probably doesn't need the Output field, remove it.

Upvotes: 1

Related Questions