-1

I'm quite new to C# and I'm getting a NullReferenceException from the following line:

CommandEntered(readedLine);

The rest of the relevant code should be this:

using System;

namespace Eksamensprojekt
{
    class StregSystemCLI : IStregSystemUI
    {
        IStregSystem stregSystem = new StregSystem();
        public event StregSystemEvent CommandEntered;

        public StregSystemCLI(IStregSystem stregSystem)
        {
            this.stregSystem = stregSystem;
        }         
        public void Start()
        {
            Console.WriteLine(stregSystem.activeProducts().ToString());
            Console.WriteLine("Enter your username and then a product ID and hit enter to buy a product. Example: 'username 49340'");
            Console.WriteLine("Alternatively, to buy multiple instances of the same product you can use: 'username amount ID' instead. Example: 'username 10 49340'");

            string readedLine = Console.ReadLine();
            Console.WriteLine("{0}", readedLine);

            if(readedLine != null)
            {
                CommandEntered(readedLine);
            }
        }
    }
}


namespace Eksamensprojekt
{
    class StregSystemCommandParser
    {
        IStregSystem stregSystem;
        IStregSystemUI stregSystemUI;

        public StregSystemCommandParser(IStregSystem stregSystem, IStregSystemUI stregSystemUI)
        {
            stregSystem = new StregSystem( );
            stregSystemUI = new StregSystemCLI(stregSystem);
            stregSystemUI.CommandEntered += ParseCommand;

        }

        void ParseCommand(string command)
        {
            ...lots of code here, that I don't think is relevant to the solution
        }
    }
}



namespace Eksamensprojekt { 
    public class Program 
    {

        static void Main(string[] args)
        {
            IStregSystem stregSystem = new StregSystem();
            IStregSystemUI ui = new StregSystemCLI(stregSystem);
            StregSystemCommandParser sc = new StregSystemCommandParser(stregSystem, ui);

            ui.Start();
        }
    }
}

namespace Eksamensprojekt
{
    public delegate void StregSystemEvent(string Command);
}

I hope the above is readable without the entire code. What I want to happen is if something is entered into readedLine and the string ain't null, I want it to trigger an event that results in the method ParseCommand() to be run in the class StregSystemCommandParser. How do I make a solution that fixes the NullReferenceException?

  • Yes the duplicate stands, but this question has something that I am not sure if the duplicate covers. It involves also the problem of passing by value a reference variable and expecting it to be initialized correctly. – Steve Apr 12 '16 at 19:45

1 Answers1

0

There is no listener attached to your CommandEntered event.

The correct C# idiom to call an event is:

CommandEntered?.Invoke(readedLine);
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Doesn't give a nullreferenceexception anymore. However, this doesn't make me enter the desired method either, sadly. Any hints on why that may be? Thanks a lot for the help btw. – user2972258 Apr 12 '16 at 20:12