Answered Exception User Unhandled System.NullReferenceExeption. Driver was null.

benc991

New member
Joined
Oct 28, 2020
Messages
4
Programming Experience
Beginner
I am currently under development of a new app for a laser printer. I am using Visual studio 2017 with Twincat integrated. My main program is in PLC but I have an Interface written in C#. I am fairly new in C# so I must be missing something obvious. I have succesfully build an app, but when I try to launch it I get an error.

Mod edit : Attachment removed. Post code in code tags, not as screenshots
 
Last edited by a moderator:
Solution
In the future, please post the text of your code in code tags, as well as the error. It is hard to view code on small devices like phones.

Anyway, the error is telling you exactly what the issue is. You are trying to access an object that is null. So either driver is null or driver.Content is null.
In the future, please post the text of your code in code tags, as well as the error. It is hard to view code on small devices like phones.

Anyway, the error is telling you exactly what the issue is. You are trying to access an object that is null. So either driver is null or driver.Content is null.
 
Solution
Also for future reference, please provide meaningful titles that summarise your issue. Everyone who posts here needs help regarding their app so your title tells us absolutely nothing about whether the thread is relevant to us.
 
Thank you for your help and comments. Noted. I am used to work in PLC so this is my first time working on such a big project. It's strange to me that the compiler didn't issue an error in this instance. I need to give an object a value that is different from zero. How can I do that? Do I need to call a type's class constructor or is my error because something else.
 
C#:
private DriverMode ModeSelect(Button Driver)
       
           
        {
            switch(Driver.Content.ToString())
            {
                case "Continuous":
                    return DriverMode.continuous;

                case "10 mm":
                    return DriverMode.mm10;

                case "5 mm":
                    return DriverMode.mm5;

                case "1 mm":
                    return DriverMode.mm1;

                case "0.1 mm":
                    return DriverMode.mm01;

                case "0.01 mm":
                    return DriverMode.mm001;

                default:
                    return DriverMode.continuous;
            }
           
        }
 
Last edited by a moderator:
As suggested, the error message is telling you that you're trying to access a member of an object that doesn;t exist. You need to debug your code. You ALWAYS need to debug your code first. Place a breakpoint on that and, when execution breaks, use the debugger to see whether Driver or Driver.Content is null and then work backwards to determine why and what you need to do to fix it.
 
Thank you I did that. The problem is as it seems Driver.Content. it states *Driver.Content threx an exception of type "SystemNullReferenceException"
 
It's strange to me that the compiler didn't issue an error in this instance.
It shouldn't be strange. The compiler does work at compile time. The error you are getting is at runtime. Most of the static analysis performed by a compiler at compile time is targeted towards performance optimizations, not logic errors. It can sometimes warn you of potentially using an object that has not yet been initialized, but it doesn't do a very deep check. There are 3rd party static analysis tools which does a deeper analysis of the program code and can uncover logic problems like this runtime error.
 
It's strange to me that the compiler didn't issue an error in this instance.
Why would it? Look at your method. It receives a Button via a parameter and then proceeds to use the Content of that in code. How is the compiler supposed to know whether you will have set the Content of that Button at run time when you call that method?
 
Back
Top Bottom