Click here to Skip to main content
15,884,700 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I am trying to rule a test on my code and I get an exception like this System.NullReferenceException: Object reference not set to an instance of an object.

Here is my code :

public class Class1
    {
        XLDriver CANDemo = new XLDriver();
        XLClass.xl_driver_config driverConfig = new XLClass.xl_driver_config();

        public int GetDescription()
        {
            XLDefine.XL_Status status;
            status = CANDemo.XL_OpenDriver();
            Console.WriteLine("Open Driver       : " + status);
            //c.GetDescription();
            status = CANDemo.XL_GetDriverConfig(ref driverConfig);
            Console.WriteLine("Get Driver Config : " + status);
            return 0;
        }
    }


And my test code :
[TestClass]
public class UnitTest1
{
    static CANInterface.Class1 can;
    [ClassInitialize]
    public static void InitClass(TestContext context)
    {
        can = new CANInterface.Class1();
    }

    [TestMethod]
    public void TestMethod1()
    {
        can.GetDescription();
    }
}


The methods from my GetDescription method are imported from a dll. And when I run the test code I get that error but from the XLDriver object and I don't know why. The XLDriver class is also from the dll. How can I resolve this problem?

Thank You.

What I have tried:

I tried to put the XLDriver object also in the method but it didn't worked.
Posted
Updated 27-Sep-17 21:46pm
v2
Comments
Karthik_Mahalingam 28-Sep-17 3:26am    
which line?
Todiruta Costel Nicusor 28-Sep-17 3:42am    
when I run the test on debug mode, it takes me to this line
XLDriver CANDemo = new XLDriver(); 
.

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Overall, that's a terribly bad code: PrintFunctionError requires a user to interact with the console. Whenever that function will be called within a Unit Test (or run on a Web Server, a Windows Service etc.), it will just hang.

A return value is not meant to indicate if an exception happened or not.

Variables / members are badly named, at first I thought that thing can't compile because I failed to notice that driverConfig is a member variable and initialized at instantiation.

Finally, these functions cannot be unit tested: a unit test asserts something, and what can be asserted here? That something was written onto the console?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900