首页 > 解决方案 > 由于遇到 StackOverFlowException 错误而终止进程,我哪里出错了?

问题描述

似乎找不到我哪里出错了,应该早点测试一下,很确定这里有一个重大的逻辑错误。我正在尝试根据用户输入从文本文件中列出国家/地区,但出现此错误,我尝试删除大部分代码并尝试输入其他测试代码,但仍然出现重大错误,不知道我做错了什么如果有人可以告诉我这里的问题是什么,那将非常感谢。基本上,如果用户输入是大写字母,该程序还应该稍后列出所有国家,但我可以稍后再谈。

class CountryProgram
{
    static void Main(string[] args)
    {
        CountryList printSearch = new CountryList();
        printSearch.findAll();
    }

    public class CountryList

    {
        //declare all variable lists for each search
        public StreamReader filePath = new StreamReader("countries.txt");
        public static List<string> countryItems = new List<string>();
        public static List<string> capitalItems = new List<string>();
        public static List<string> populationItems = new List<string>();
        public static List<string> allItems = new List<string>();


        Country memberName = new Country();

        //string retriever read all inputs
        public string getString()
        {
            //<<<<<==============Country Search===================>>>>>
            Console.WriteLine("Enter the first letter of the countries you wish to search: "); //prompts user to enter the country
            string upperCaseInput = Console.ReadLine(); //gets  input
            string line = ""; //creates a empty string
            bool finished = false;

            List<string> upperList = new List<string>(); //creates a list for user input chars

            foreach (char item in upperCaseInput) //adds all chars into a list named chars
            {
                if ( Char.IsUpper(item)) //checks if upper case and add to list if true
                {
                    upperList.Add(item.ToString());
                    memberName.startsWith = true;      //Assigns startsWith to true the moment a char entered is uppercase
                }
            }

            //Begin listing countries once startsWith == true
            while (((line = filePath.ReadLine()) != null) && (finished = false))
            {
                if (memberName.startsWith == true) // if upper case in inputted loop through the first column listing countries
                {
                    countryItems.Add("test output"); //supposed to write results based on inputted upper case char item above
                }

                else //if not upper case show error message and end program
                {
                    finished = true;
                }
                if (finished == true)
                {
                    Console.WriteLine("Please enter one character in capital letters");
                    Console.ReadLine();
                }
            }
            //<<<<<==============Capital Search===================>>>>>//
            return countryItems.ToString();
        }

        //executing write to control panel depending on userinput
        public void findAll()
        {

            Console.WriteLine(memberName.getString());
            Console.ReadLine();
        }
    }

    class Country : CountryList
        {
        //list variable properties
        public string name { get; set; }
        public string capital { get; set; }
        public int population { get; set; }
        public bool startsWith { get; set; }
        public bool capitalHas { get; set; }
        public bool lesserPopulation { get; set; }

        //list counstructors
        public Country()
            {
            }
        public Country(string n, string c, int p)
        {
            name = n;
            capital = c;
            population = p;
        }
    }

标签: c#visual-studio

解决方案


Country源自CountryList。当您尝试创建 的实例时CountryList,它还会尝试创建Country(用于memberName字段)的项目。但是,Country 包含一个memberName字段,也需要创建它。对于 的每个实例Country,您都会创建另一个实例,Country直到您用完堆栈空间 - StackOverflowException

你真的没有理由想要CountryCountryList. 如果CountryList应该是国家列表,只需让列表包含项目即可。


推荐阅读