08-12-2016 05:03 PM
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();
}
08-15-2016 05:07 PM
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?
08-15-2016 06:04 PM
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();
}
}
}
08-16-2016 02:36 PM
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
08-18-2016 10:04 AM
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
08-22-2016 09:50 AM
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?
08-22-2016 09:57 AM
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?
08-23-2016 03:36 PM
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.
08-23-2016 05:31 PM
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.
08-24-2016 03:19 PM
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?