Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I run some code (WIP) and I get a typeinitializationexception index out of range. I'm not very experienced with code so this one kinda stumped me. It tells me that the issue is something with "player.player()" and it occurs when it gets to the for loop in AddTurns(). I need some help.


<pre>class game
    {

        public static List<Player> PlayerList = new List<Player>();
        public static List<Player> Playerlist 
        {
        
            get { return PlayerList; }
        
        }

        public static int playercount;

        public static int PlayerCount 
        {

            get { return playercount; }
            set { playercount = value; }

        }
        public void PlayGame()
        {

            Console.WriteLine("Välkommen och låt spelet börja!");
            Console.WriteLine("För att börja spelet, var vänlig ange antal spelare och spelarnas namn");
            Console.WriteLine("Skriv in antal spelare: ");
            PlayerCount = int.Parse(Console.ReadLine());
            Console.WriteLine("Okej! Du har angivit " + PlayerCount + " spelare.");
            for (int n = 1; n < PlayerCount + 1; n++)
            {

                Console.WriteLine("Skriv nu in namn för spelare " + n + ":");
                PlayerList.Add(new Player() { PlayerName = Console.ReadLine() });

            }

            Console.WriteLine("Ok, välkomna till spelet!");
            Console.WriteLine("Den spelare vars tur det är får kasta tre gånger");
            Console.WriteLine("genom att skriva 3 heltal");
            Player playeraccess = new Player();
            playeraccess.AddTurns();

        }

        public void winstate()
        {

            Console.WriteLine("Grattis till vinnaren!");
            Console.WriteLine("Du har lyckats samla 301 poäng!");
            Console.WriteLine("Vill ni spela igen? Y/N.");
            for (int exitloop = 0; exitloop == 1; exitloop++)
            {

                char restart = Console.ReadKey().KeyChar;
                if (restart == 'y')
                {

                    PlayerCount = 0;
                    PlayerList.Clear();
                    PlayGame();

                }

                else if (restart == 'n')
                {

                    Environment.Exit(0);

                }

                else
                {

                    Console.WriteLine("Försök igen, något gick snett");
                    --exitloop;

                }

            }


        }

    }
    class Player
    {
        public static int turnloop = 0;
        public static List<Turns> playerturn = new List<Turns>();
        public static List<Player> players = game.PlayerList;
        public static Player player = players[Player.turnloop];
        public static List<Turns> turns = Player.playerturn;
        public static Turns turn = Player.turns[Player.playerturn.Count];
        public static int firstruncheck = 0;
        public static int turncounter = 0;
        private int throw1;
        public int Throw1
        {

            get { return throw1; }
            set { throw1 = value; }

        }
        private int throw2;
        public int Throw2
        {

            get { return throw2; }
            set { throw2 = value; }

        }
        private int throw3;
        public int Throw3
        {

            get { return throw3; }
            set { throw3 = value; }

        }
        public List<Player> playerS
        {

            get { return players; }
            set { players = value; }

        }
        public Player playeR
        {

            get { return player; }
            set { player = value; }

        }
        public List<Turns> PlayerTurn
        {

            get { return playerturn; }
            set { playerturn = value; }

        }
        public static List<Turns> turnS
        {

            get { return turns; }
            set { turns = value; }

        }
        public Turns turN
        {

            get { return turn; }
            set { turn = value; }

        }
        public Player(string playername = "")
        {

            PlayerName = playername;

        }

        public string PlayerName { get; set; }

        public override string ToString()
        {
            return base.ToString();
        }

        public void CalculatePoints()
        {

            int calcpoints = Player.turn.Throwscore1 + Player.turn.Throwscore2 + Player.turn.Throwscore3;
            Console.WriteLine("Du har " + calcpoints + " Poäng totalt.");
            if (calcpoints >= 301)
            {

                game gameaccess = new game();
                gameaccess.winstate();

            }

            else
            {

                calcpoints = 0;
                AddTurns();

            }

        }
        public void AddTurns()
        {

            for(turnlooper = 0; turnlooper < game.PlayerCount; turnlooper++) 
            {

                Player.turns.Add(new Turns());
                Throw1 = 0;
                Throw2 = 0;
                Throw3 = 0;
                Console.WriteLine("spelare " + (turnloop + 1) + ", skriv in ditt första kast.");
                Throw1 = int.Parse(Console.ReadLine());
                Console.WriteLine("spelare " + (turnloop + 1) + ", skriv in ditt andra kast.");
                Throw2 = int.Parse(Console.ReadLine());
                Console.WriteLine("spelare " + (turnloop + 1) + ", Skriv in ditt sista kast.");
                Throw3 = int.Parse(Console.ReadLine());
                if (firstruncheck == 0)
                {

                    firstruncheck++;
                    
                    playerturn[turncounter].Throwscore1 = playerturn[turncounter].Throwscore1 + Throw1;
                    playerturn[turncounter].Throwscore2 = playerturn[turncounter].Throwscore2 + Throw2;
                    playerturn[turncounter].Throwscore3 = playerturn[turncounter].Throwscore3 + Throw3;
                    Queery();

                }

                else 
                {

                    playerturn[turncounter].Throwscore1 = playerturn[turncounter - 1].Throwscore1 + Throw1;
                    playerturn[turncounter].Throwscore2 = playerturn[turncounter - 1].Throwscore2 + Throw2;
                    playerturn[turncounter].Throwscore3 = playerturn[turncounter - 1].Throwscore3 + Throw3;
                    Queery();

                } 
            
            }

        }

        public void Queery() 
        {

            char queery;
            
            for (int queeryloop = 0; queeryloop < 1; queeryloop++) 
            {

                Console.WriteLine("Vill du se dina poäng? Y/N");
                queery = Console.ReadKey().KeyChar;
                if (queery == 'y') 
                {

                    queeryloop = 0;
                    Turns turnsaccess = new Turns();
                    turnsaccess.GetScore();
                
                }

                else if (queery == 'n')
                {

                    queeryloop = 0;
                    CalculatePoints();
                
                }

            }
        
        }

    }

    public class Turns
    {

        public Turns(int turn1 = 0, int turn2 = 0, int turn3 = 0, int turntotal = 0)
        {

            Throwscore1 = turn1;
            Throwscore2 = turn2;
            Throwscore3 = turn3;
            Throwscoretotal = turntotal;

        }
        public int Throwscore1 { get; set; }
        public int Throwscore2 { get; set; }
        public int Throwscore3 { get; set; }
        public int Throwscoretotal { get; set; }


What I have tried:

I don't even know what the issue is so i'm just kinda confused.
Posted
Updated 29-Mar-21 22:56pm
Comments
[no name] 29-Mar-21 18:27pm    
For starters, you've duplicated the (public) PlayerList name. The first instance should at least be a different spelling (and referred to in the second instance).
Richard MacCutchan 30-Mar-21 3:41am    
Which line does the error occur on? Please do not expect people to guess what your code does.
Borkanders bork 30-Mar-21 11:51am    
Like I stated in my explanation it occurs in the for loop in AddTurns() and it refers back to "player.player()".

1 solution

Quote:
C#
public static List<Player> PlayerList = new List<Player>(); // PlayerList.Count === 0
...
public static int turnloop = 0;
public static List<Player> players = game.PlayerList; // players.Count === 0
public static Player player = players[Player.turnloop]; // BOOM!
You are trying to access the first item of an empty list. This naturally throws an exception.

You need to fix your logic - add at least one player to the list before trying to read the first player.
 
Share this answer
 
v2
Comments
Borkanders bork 30-Mar-21 11:53am    
In the method PlayGame() there is a loop that adds Players to the list and then assigns a name to them. It just doesn't seem to register any Players.
Richard Deeming 30-Mar-21 11:55am    
Your static field initializers run before any instance of the class has been created. Your method has not executed when you try to retrieve the first item from the players list, so the players list is empty.
Borkanders bork 30-Mar-21 12:03pm    
Ok that makes sense! So I should simply remove the unecessary static modifier and change my code to accommodate?
Richard Deeming 30-Mar-21 13:36pm    
I don't see anything that obviously needs to be static, but there's a lot of code that needs to be refactored. For example, most of the fields on the player class seem to be related to the game itself, not the individual player.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900