user29749222
user29749222

Reputation: 1

Activator.CreateInstance(Type type) throws System.ArgumentNullException on parameter type

(wanted to clarify that I'm doing a little personal project to learn OOP with C#, a little text rpg for the Console without a game engine)

So basically I have a CSV file with a bunch of objects and one of the columns is the type of the object (all the types are children of one parent class). I stumbled upon Activator.CreateInstance(Type type) because i can't use new since i don't know what class the object is supposed to be.

The problem is that it says the parameter type is null even though it is not.

I have something like

//CSVList being a list of string arrays with the type of the object in [0]
string typename = CSVList[index][0];
Type type = Type.GetType(typename);
ParentClass objectExample;
objectExample = (ParentClass)Activator.CreateInstance(type);                        

I used

Console.WriteLine(typename);
Console.WriteLine(type != null);

to confirm that type is in fact, not null. But it still throws:

System.ArgumentNullException: Value cannot be null (Parameter 'type')
at System.ArgumentNullException.Throw(String paramName)
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)

Upvotes: 0

Views: 48

Answers (0)

Related Questions