私はC#にはかなり新しいですし、私は次の行からとNullReferenceExceptionを取得しています:私はとNullReferenceExceptionやイベント
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);
}
:関連するコードの残りの部分は、このする必要があります
CommandEntered(readedLine);
コード全体がなくても上記を読むことができれば幸いです。 何が起こるかは、readedLineに何かが入力され、文字列がnullでない場合、ParseCommand()メソッドがStregSystemCommandParserクラスで実行されるイベントをトリガーする必要があります。 NullReferenceExceptionを修正するソリューションを作成するにはどうすればよいですか?
はい重複が立ってますが、この質問は、私は重複カバーする場合を確認していない何かを持っています。また、参照変数を値渡しし、それが正しく初期化されることを期待するという問題もあります。 – Steve