Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

First.Value of LinkedList is null but linked list is not empty

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):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();
        }
    }
}

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading