Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Null Reference Exception in C#.NET

Null Reference Exception in C#.NET

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpquestionhelp
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    I explore code
    wrote on last edited by
    #1

    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??

    C C B 3 Replies Last reply
    0
    • I I explore code

      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??

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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

      I 1 Reply Last reply
      0
      • I I explore code

        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??

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        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 )

        1 Reply Last reply
        0
        • I I explore code

          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??

          B Offline
          B Offline
          Brady Kelly
          wrote on last edited by
          #4

          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.

          S 1 Reply Last reply
          0
          • B Brady Kelly

            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.

            S Offline
            S Offline
            sarah_malik
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • C Colin Angus Mackay

              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

              I Offline
              I Offline
              I explore code
              wrote on last edited by
              #6

              Thankx a lot for clearing my doubt

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups