Null Reference Exception in C#.NET
-
Some times during the execution of C#.NET programs I encounter this error which states "Object Reference Not set to an instance of object" and then the program terminates if there is no exception handler code. I need to know, what is the meaning of this exception??
-
Some times during the execution of C#.NET programs I encounter this error which states "Object Reference Not set to an instance of object" and then the program terminates if there is no exception handler code. I need to know, what is the meaning of this exception??
gladiatron wrote:
Some times during the execution of C#.NET programs I encounter this error which states "Object Reference Not set to an instance of object" and then the program terminates if there is no exception handler code. I need to know, what is the meaning of this exception??
I think the error message is self explanatory. You have an object reference (a variable, if you prefer) that is set to null (not an instance of an object). While null variables are perfectly acceptable in and of themselves, if you attempt to use them (e.g. call a method on them) it will fail.
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
-
Some times during the execution of C#.NET programs I encounter this error which states "Object Reference Not set to an instance of object" and then the program terminates if there is no exception handler code. I need to know, what is the meaning of this exception??
string s = null; string d = s.ToLower(); This will compile, but generate that error.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Some times during the execution of C#.NET programs I encounter this error which states "Object Reference Not set to an instance of object" and then the program terminates if there is no exception handler code. I need to know, what is the meaning of this exception??
Set your debugger to break on the exception. It will then break on the line that causes the exception instead of the exception being caught at top level by the debugger, where it has no option but to terminate. You can do this by selecting the Debug->Exceptions menu item. You get a dialogue that says, "Break when exception is...", then expand CLR Exceptions and find System.NullReferenceException. Check the flag for Thrown. The debugger will now break when the exception is thrown, and not only when it isn't handled.
-
Set your debugger to break on the exception. It will then break on the line that causes the exception instead of the exception being caught at top level by the debugger, where it has no option but to terminate. You can do this by selecting the Debug->Exceptions menu item. You get a dialogue that says, "Break when exception is...", then expand CLR Exceptions and find System.NullReferenceException. Check the flag for Thrown. The debugger will now break when the exception is thrown, and not only when it isn't handled.
I encountered the same one when I forgot to allocate the memory to a variable. if you are using an object from a class do not forget the new(); or gcnew(); keyword.
-
gladiatron wrote:
Some times during the execution of C#.NET programs I encounter this error which states "Object Reference Not set to an instance of object" and then the program terminates if there is no exception handler code. I need to know, what is the meaning of this exception??
I think the error message is self explanatory. You have an object reference (a variable, if you prefer) that is set to null (not an instance of an object). While null variables are perfectly acceptable in and of themselves, if you attempt to use them (e.g. call a method on them) it will fail.
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
Thankx a lot for clearing my doubt