0

I'm using VS 2010. I created a C# Class and compiled it to a DLL, then added a windows forms project to it to test the dll. I added a Reference to the DLL in the TestApp project in the Solution Explorer. Both projects compile without errors. But when I run the TestApp, I get a NullReferenceException when I call a method in the dll. The error says there is no instance of the object, which I unsterstand to be the method being called.

Everything I've done is the same as other DLL examples I've found on the internet. For example: http://msdn.microsoft.com/en-us/library/3707x96z(v=vs.100).aspx

http://coderock.net/how-to-create-a-dll-file-in-visual-studio-2010/

But I've obviously missed something basic.

// DLL Project

namespace SLink
{
    public class AmpAPI
    {
        public String[] ReadID( Int32 id )
        {
            String[] result = new string[] { "A", "B", "C", "D" };
            return result;
        }
    }
}

// Test Application

using SLink;

namespace TestApp
{
    public partial class frmMain : Form
    {
        AmpAPI amp;

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load( object sender, EventArgs e )
        {
            amp = new AmpAPI();
        }

        private void btnUpdate_Click( object sender, EventArgs e )
        {
            String[] result = new String[] { "", "", "", "" };
            result = amp.ReadID( 0 ); // <-- NullReferenceException
        }
    }
}
1
  • What's the stack trace? What do you see in the debugger? Commented May 14, 2012 at 22:35

2 Answers 2

3

It looks like your frmMain_Load method is not being called when the form loads.

Did you just copy and paste the code? That's not enough to register the method as an event handler for the Load event.

  • You can add a new handler to the form's Load event by double clicking the form in the designer. Then you can edit the body of the method.
  • Alternatively you can choose an existing method to handle the event. To do this click the form in the designer, go to properties, select events, find Load, then select your method from the drop-down list.

Add handler to Load event in WinForms designer

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

    private void btnUpdate_Click( object sender, EventArgs e )
    {
        String[] result = amp.ReadID( 0 );
    }

Not sure if it is liking your previous implementation for initializing the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.