0

I have this small piece of code:

public static I CreateInstance<I>(string myClassName) where I : class
{
    Debug.Log("Instance: " + (Activator.CreateInstance(null, myClassName) as I));
    return Activator.CreateInstance(null, myClassName) as I;
}

void Test(string testName)
{
    testName = "TestProvider";
    var data = CreateInstance<IProviderInitializer>(testName).GetProviderInfo();
    Debug.Log("Data: " + data);
}

And the problem is that I get NULL Reference Exception and I have no idea why.

weaverbeaver
  • 151
  • 2
  • 10
  • Why are you inserting a class via a string? Why not `public static T CreateInstance(T myClass) where T : class`? – Frontear Oct 23 '18 at 23:10
  • 1
    Are you sure `testName` is of type `IProviderInitialzer". That would be a reason you're getting null – MikeH Oct 23 '18 at 23:10
  • 1
    You probably don't want to create two. Store the result in a variable and use that variable when you write your log message. – Retired Ninja Oct 23 '18 at 23:14
  • Frontear: Because it's actually part of a longer code, where i have a Dictionary, key being the name of the classes(there will be more elements) and the value is what those classes returned(in my case, GetProvierInfo() method). It's not really my choice, this is how I have to do it. @MikeH TestProvider inherits both ProviderManager(which is the script containing these 2 methods) and IProviderInitializer, you think it might be a problem? Retired Ninja: thanks, but sadly it still returns null. – weaverbeaver Oct 23 '18 at 23:31
  • Where is your null ref exception? – MikeH Oct 23 '18 at 23:41
  • Where precisely do you get the Null Reference Exception? Is the exception caused because _CreateInstance_ return nulls and thus _GetProviderInfo_ cannot be called, or is the exception originating somewhere in the implementation of the _GetProviderInfo_ method? The strack trace provided by the exception will tell you... –  Oct 23 '18 at 23:41
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Oct 24 '18 at 00:11
  • I was getting it at the CreateInstance method, the answer was that I had to unwrap my object, for the code see my reply to the other answer. Thanks a lot guys! – weaverbeaver Oct 25 '18 at 16:57

3 Answers3

5

Never use x as T when you expect x to be T. Always use (T) x instead in that case. That way you can detect mistakes where you happen to have the wrong type for an object early, instead of hiding them behind NullReferenceExceptions.

AyCe
  • 727
  • 2
  • 11
  • 30
2

Rather than using the overload that takes the type as a string (and returns a handle to an object), you could create a Type object and pass that to the overload of CreateInstance that accepts a Type:

Type t = Type.GetType(myClassName);
return Activator.CreateInstance(t) as I;
D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

From the Documentation

public static System.Runtime.Remoting.ObjectHandle CreateInstance (string assemblyName, string typeName);

The CreateInstance method call return "ObjectHandle" type which is not convertable to "I", Activator.CreateInstance(null, myClassName) as I will always return null.

You need unwarp your object

public static void Main()
   {
      ObjectHandle handle = Activator.CreateInstance("PersonInfo", "Person");
      Person p = (Person) handle.Unwrap();
      p.Name = "Samuel";
      Console.WriteLine(p);
   }
D Stanley
  • 149,601
  • 11
  • 178
  • 240
LeY
  • 659
  • 7
  • 21
  • Actually this was part of the answer, I had to unwrap my object. Here is the complete code if anyone needs it: `static I CreateInstance(string myClassName) where I : class { var createdInstance = Activator.CreateInstance(null, myClassName); return createdInstance.Unwrap() as I; }` – weaverbeaver Oct 25 '18 at 16:55