samedi 26 août 2017

System.NullReferenceException while trying to create a reference type of an already existing class

so I'm creating an console application and I've decided to build it using the command pattern. So simply I have an interface /*ICommand */, an abstract class that inherits the interface called AbstractCommand and several classes that inherits the abstract one with names of type {commandName}Command (for example the command to quit the app is QuitCommand). I'm not quite sure whether you got what I'm trying to say, however, I'll just give the code I get an error with.

private string ProcessInput(List<string> arguments)
    {
        string command = arguments[0];
        arguments.RemoveAt(0);

        Type commandType = Type.GetType(command + "Command");
        var constructor = commandType.GetConstructor(new Type[] { typeof(IList<string>), typeof(IManager) });
        ICommand cmd = (ICommand)constructor.Invoke(new object[] { arguments, this.fighterManager });
        return cmd.Execute();
    }

So the main problem is that when I get to the line "Type commandType = ...", for some reason commandType just gets set to null, not for let's say QuitCommand and therefore the next line just throws an "System.NullReferenceException: 'Object reference not set to an instance of an object.'"

I don't have enough reputation to post images, so here is the link : http://ift.tt/2xD2ppw !

I tried to mark that the "command" has saved the command name, however, commandType is still null :(





Aucun commentaire:

Enregistrer un commentaire