Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack overflow exception—why?

I keep getting a stack overflow exception with this. I've narrowed it down to this class to try and figure out what was wrong but I simply have no idea why I keep getting this error message? Originally I have the user interface in another class but just to eliminate everything else like problems in my calling methods I moved the essentials to this class to try and figure out what was wrong. I thought it might be my properties? Maybe it's obvious to everyone else but I simply don´t understand.

Since I am very new to programming I would appreciate some help on what I have done wrong. From what I understand this problem occurs when you have something like an infinite loop?

namespace MyNameSpace
{
    public class Customers
    {
        private List<Customers> customers; 

        public Customers()
        {
            customers = new List<Customers>();

            AddCustomer(new Customers() 
            { 
            Name = "A", Telephone="1" 
            });
        }

        public string Name
        {
            get;
            set;
        }
        public string Telephone
        {
            get;
            set;
        }

        public void RunTest()
        {

            Console.WriteLine();
            Console.WriteLine("****** VIDEOSTORE ******");
            Console.WriteLine();
            Console.WriteLine("1. Show Customers");
            Console.WriteLine("6. Quit");

            string userChoice = Console.ReadLine();

            switch (userChoice)
            {
                case "1": 
                    View();
                    break;         

                    break;
                case "2":
                    break;
            }
        }

        public void View()
        {
            foreach (Customers c in customers)
            {
                Console.WriteLine();
                Console.WriteLine(c.Name);
                Console.WriteLine(c.Telephone);
                Console.WriteLine();
            }
        }

        public void AddCustomer(Customers custom)                           
        {
            customers.Add(custom);          
        }
    }
}
like image 979
user1153537 Avatar asked Jan 22 '12 16:01

user1153537


4 Answers

In the Customers constructor you calling the Customers constructor again, creating infinite recursion.

You should have a separate class for a list of Customers and for a single Customer:

namespace MyNameSpace
{
    public class Customer
    {
        public string Name
        {
            get;
            set;
        }
        public string Telephone
        {
            get;
            set;
        }
    }

    public class Customers
    {
        private List<Customer> customers; 

        public Customers()
        {
            customers = new List<Customer>();

            AddCustomer(new Customer() 
            { 
            Name = "A", Telephone="1" 
            });
        }


        public void RunTest()
        {

            Console.WriteLine();
            Console.WriteLine("****** VIDEOSTORE ******");
            Console.WriteLine();
            Console.WriteLine("1. Show Customers");
            Console.WriteLine("6. Quit");

            string userChoice = Console.ReadLine();

            switch (userChoice)
            {
                case "1": 
                    View();
                    break;         

                    break;
                case "2":
                    break;
            }
        }

        public void View()
        {
            foreach (Customer c in customers)
            {
                Console.WriteLine();
                Console.WriteLine(c.Name);
                Console.WriteLine(c.Telephone);
                Console.WriteLine();
            }
        }

        public void AddCustomer(Customer customer)                           
        {
            customers.Add(customer);          
        }
    }
}
like image 194
Damir Arh Avatar answered Oct 06 '22 11:10

Damir Arh


You are calling your creating a new Customers object in the Customers constructor.

like image 41
jcopenha Avatar answered Oct 06 '22 11:10

jcopenha


Your constructor for Customers calls itself, causing an endless loop.

public Customers()
{
    customers = new List<Customers>();

    AddCustomer(new Customers() // <- Here
    { 
    Name = "A", Telephone="1" 
    });
}

Endless recursive calls to a function will cause a StackOverFlow.

like image 2
ARRG Avatar answered Oct 06 '22 11:10

ARRG


You are instantiating a List within the constructor of your Customers class. This will cause an infinite loop and result in a stack overflow.

I think you should try to separate your code into multiple classes.

public class Customer
{
    public string Name { get; set; }
    public string Telephone { get; set; }
}

public class Program
{
    private List<Customer> _customers = new List<Customer();

    public Program()
    {
        _customers.Add(new Customer() 
        { 
            Name = "A", Telephone="1" 
        });
    }

    // your other methods here - like View()
}
like image 1
kfuglsang Avatar answered Oct 06 '22 09:10

kfuglsang