From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

NullReferenceException using LabVIEW DLL with ASP.NET MVC

I am using ASP.NET MVC project with DLL built with LabVIEW 2015. The first call to any function would cause NullReferenceException. If the first exception was caught and ignored, subsequent call would no longer cause any exception. Below is the code snippet in HomeController.cs. Ping() is an empty vi.

 

using System.Runtime.InteropServices;
using foo;
using NationalInstruments.LabVIEW.Interop;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            foo__32API.Ping(); // An exception of type 'System.NullReferenceException' occurred in foo.dll but was not handled in user code

            return View();
        }

0 Kudos
Message 1 of 11
(5,313 Views)

Hi CyclePanda,

 

Can you give me some more information on the DLL? Does the DLL work when called in LabVIEW? Have you tested all the functions in LabVIEW to make sure they all return what you expect?

-----------------------------------------------
Brandon Grey
Certified LabVIEW Architect

Message 2 of 11
(5,240 Views)

The DLL is created as .NET Interop Assembly. I have attached a test project I created and tested with. This DLL is used in a ASP.NET project in the HomeController.cs of the template as follow:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using InteropAssembly;

using NationalInstruments.LabVIEW.Interop;

namespace WebApplication1.Controllers

{

public class HomeController : Controller

{

public ActionResult Index()

{

InteropAssembly.TestLibrary.Test(); // Null Reference Exception on first call

return View();

}

public ActionResult About()

{

ViewBag.Message = "Your application description page.";

return View();

}

public ActionResult Contact()

{

ViewBag.Message = "Your contact page.";

return View();

}

}

}

 

0 Kudos
Message 3 of 11
(5,233 Views)

Hey CyclePanda,

 

What version of .NET are you working with? Does this happen on each and every call to a function in the NationalInstruments.LabVIEW.Interop class? Can you tell me where you have this DLL on your computer?

 

Finally, have you had a chance to look at this help file already and followed it when creating your Interop assembly?

 

Building a .NET Interop Assembly 

Mathew H.
Applications Engineer
National Instruments
0 Kudos
Message 4 of 11
(5,211 Views)

1) The .NET version is 4.5.2.

2) The NullReferenceException is thrown only the first time. No exception is thrown if I catch the exception and call the function again.

public ActionResult Index()

{

try

{

InteropAssembly.TestLibrary.Test();

}

catch (Exception ex)

{

}

InteropAssembly.TestLibrary.Test();

return View();

}

 

3) The LV Dll refrenced is in D:\Data\Misc\Temp\Test\Builds

0 Kudos
Message 5 of 11
(5,188 Views)

I think the problem may be caused by the LabVIEW DLL was not loaded or initialized when it is first called. I want to create an instance of the class first before invoking the method. How do I export the LV class to .NET so it can be instantiated as an object?

0 Kudos
Message 6 of 11
(5,138 Views)

I think the NullReferenceException occurred because the LabVIEW DLL was not loaded the first time the function is called. I want to create an instance of the LabVIEW class before invoking the method.
How do I export a LabVIEW class to .NET so an instance can be created?

0 Kudos
Message 7 of 11
(5,135 Views)

Do you have the DLL referenced in the project? Or are you just putting "Using NationalInstruments.LabVIEW.Interop;"? Or did you create a reference to the DLL in your project itself as well?

 

You have to include the reference in the project or else when you say you're trying to use methods in that class, it won't know where they are.

Mathew H.
Applications Engineer
National Instruments
0 Kudos
Message 8 of 11
(5,104 Views)

Both the LV dll and NationalInstruments.LabVIEW.Interop are added to the project reference and included in the namespace. When I type the LV dll name, it will display the methods in the drop down.

0 Kudos
Message 9 of 11
(5,096 Views)

Hi CyclePanda,

 

I'm still not able to recreate this issue. I used the following code to just call the Test VI in the Interop library, but the project runs fine with all the correct references.

 

Do you think it has to do with the structure of your C# code somehow?

 

using System;
using System.Diagnostics;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InteropAssembly;
using NationalInstruments.LabVIEW.Interop;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
InteropAssembly.TestLibrary.Test();
Thread.Sleep(3000);
}
}
}

 

Do you see the same exception thrown using just this code?

Mathew H.
Applications Engineer
National Instruments
0 Kudos
Message 10 of 11
(5,074 Views)