Kay van Bree
Kay van Bree

Reputation: 2082

System.NullReferenceException - but Debugger shows value is non-null

I'm working on a research paper about plugin-architecture and I'm trying to recreate this code. Now I got everything right and my dll file is in the right directory, but when I run my code I get an System.NullReferenceException. This is, off cource, not an uncommon Exception for me to see, but there's something odd about it. Normally when I see this exception I add a couple of breakpoint and see where it's breaking, so that's just what I did and then I found out that, at the point where the Exception is thrown, the object is not null! Can anybody explain this?

This is the code I'd like to run:

            string args = Path.GetFileNameWithoutExtension(file);

            Type oType = null;
            try
            {
                Assembly asm = Assembly.LoadFile(file);

                if (asm == null) return;
                Type[] types = asm.GetTypes();
                foreach (Type t in types)
                {
                    // CHECK IF CURRENT TYPE IMPLEMENTS INTERFACE IPlugin
                    if (typeof(IPlugin).IsAssignableFrom(t))
                    {
                        IPlugin plugin = (IPlugin)Activator.CreateInstance(t);
                        plugin.Host = this;
                        plugins.Add(plugin);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

This is the exception being thrown: The exception

The object that is not null:

The object

One more thing. I think it might have something to do with 'this' not yet being initialized, but I can't figure out how to call this method after my constructor's finished. I tried using various events to call my function, but they didn't make anything better.

[edit]

The Host property:

// Register the plugin if host is set
    public IPluginHost Host { 
        get
        {
            return host;
        }
        set 
        { 
            host = value;
            host.Register(this); 
        } 
    }

[another edit] Apparently the code's breaking in the plugins.Add(plugin) line!

public bool Register(IPlugin plugin)
    {
        plugins.Add(plugin);
        return true;
    }

Upvotes: 3

Views: 1859

Answers (1)

Michael Stum
Michael Stum

Reputation: 180934

The TargetSite is listed as Boolean Register(Plugin_api.Plugin), so that's where the NullReferenceException is thrown. Set a breakpoint in your plugins.Add method, it would appear that your internal backing List is null, not the plugin you try to put in it :)

Edit: after seeing your update, it appears that plugins is Null from all that I can see. Is plugins a simple List? Or a custom class? What does Add do? Set a breakpoint in that Register line and see what it does. In case you initialize Plugins through a static/global object there may also be a Heisenbug (a bug that doesn't appear under the debugger because the debugger changes the state of your app), but that's really just a guess.

Upvotes: 2

Related Questions