Home > Back-end >  First.Value of LinkedList is null but linked list is not empty
First.Value of LinkedList is null but linked list is not empty

Time:07-17

I am programming a console game but I get an error on trying to use LinkedList.First.Value as it returns null. However, the list is not empty as I set so that the first entry is 55.

 LinkedList<int> Pos = new LinkedList<int>();

Main :

var p = new Program();
        p.Pos.AddFirst(55);
        Timer myTimer = new Timer();
        myTimer.Elapsed  = new ElapsedEventHandler(Move);
        myTimer.Interval = 1000;
        myTimer.Start();
        

Error Causing code (Move):

p.Pos.AddFirst(p.Pos.First.Value   p.dir);

System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Collections.Generic.LinkedList.First.get returned null.

Full code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Collections;

namespace Snake
{
    class Program
    {
        LinkedList<int> Pos = new LinkedList<int>();
        int dir = 1;
        bool snakeAlive = true;
        static void Main(string[] args)
        {
            var p = new Program();
            p.Pos.AddLast(55);
            Timer myTimer = new Timer();
            myTimer.Elapsed  = new ElapsedEventHandler(Move);
            myTimer.Interval = 1000; // 1000 ms is one second
            myTimer.Start();
            do
            {
                switch (Console.ReadKey().Key)
                {
                    case ConsoleKey.UpArrow:
                        p.dir = -10;
                        break;
                    case ConsoleKey.DownArrow:
                        p.dir = 10;
                        break;
                    case ConsoleKey.LeftArrow:
                        p.dir = -1;
                        break;
                    case ConsoleKey.RightArrow:
                        p.dir = 1;
                        break;
                }
            } while (p.snakeAlive == true);
        }

        void drawGame()
        {
            Console.Clear();
            for (int i = 0; i < 100; i  )
            {
                if (i % 10 == 9)
                {
                    Console.WriteLine(IsSnake(Pos, i));
                }
                else
                {
                    Console.Write(IsSnake(Pos, i));
                }
            }
        }
        String IsSnake(LinkedList<int> sPos, int position)
        {
            for (int i = 0; i < sPos.Count; i  )
            {
                if (position == sPos.ElementAt(i))
                {
                    return "==";
                }
            }
            return "[]";
        }
        public static void Move(object source, ElapsedEventArgs e)  {
            var p = new Program();
                p.Pos.AddFirst(p.Pos.First.Value   p.dir);
                p.Pos.RemoveLast();
            p.drawGame();
        }
    }
}

CodePudding user response:

var p = new Program(); in the Move event handler will create a new instance of your Program class which is not related to the instance you created before. Its Pos list will be empty.

You could make the Move method an instance methof (remove the static) and change the event subscription to myTimer.Elapsed = new ElapsedEventHandler(p.Move);

Within the Move method you can then directly use Pos.

  • Related