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?