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. C#
  4. Null reference exception

Null reference exception

Scheduled Pinned Locked Moved C#
debuggingquestion
7 Posts 4 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.
  • E Offline
    E Offline
    error1408
    wrote on last edited by
    #1

    Hi, when i run this code i get a null-reference exception at the last line of code. I don't know why this happens, because i first add the node and it IS there. I looked in the debugger. Does someone know a solution? foreach (DateTime d in daten) { if (treeView1.Nodes.Find(d.Month.ToString(), false) != null) { treeView1.Nodes.Add(new TreeNode(d.Month.ToString())); } treeView1.Nodes[d.Month.ToString()].Nodes.Add(new TreeNode(d.Date.ToShortDateString())); }

    E E 2 Replies Last reply
    0
    • E error1408

      Hi, when i run this code i get a null-reference exception at the last line of code. I don't know why this happens, because i first add the node and it IS there. I looked in the debugger. Does someone know a solution? foreach (DateTime d in daten) { if (treeView1.Nodes.Find(d.Month.ToString(), false) != null) { treeView1.Nodes.Add(new TreeNode(d.Month.ToString())); } treeView1.Nodes[d.Month.ToString()].Nodes.Add(new TreeNode(d.Date.ToShortDateString())); }

      E Offline
      E Offline
      Edmundisme
      wrote on last edited by
      #2

      It will be easier to see what's going on if you break out that last line of code as follows: TreeNode node = treeView1.Nodes[d.Month.ToString()]; TreeNode newNode = new TreeNode(d.Date.ToShortDateString()); node.Nodes.Add(newNode); This will allow you to check state as you step though in debug mode. When you put so much into a single line, it can be much less obvious what is breaking.

      1 Reply Last reply
      0
      • E error1408

        Hi, when i run this code i get a null-reference exception at the last line of code. I don't know why this happens, because i first add the node and it IS there. I looked in the debugger. Does someone know a solution? foreach (DateTime d in daten) { if (treeView1.Nodes.Find(d.Month.ToString(), false) != null) { treeView1.Nodes.Add(new TreeNode(d.Month.ToString())); } treeView1.Nodes[d.Month.ToString()].Nodes.Add(new TreeNode(d.Date.ToShortDateString())); }

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #3

        if d is null from daten then d.Month will fail; Also, treeView1.Notes[someString] may return null as well but it is most likely the d;


        File Not Found

        G 1 Reply Last reply
        0
        • E Ennis Ray Lynch Jr

          if d is null from daten then d.Month will fail; Also, treeView1.Notes[someString] may return null as well but it is most likely the d;


          File Not Found

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Ennis Ray Lynch, Jr. wrote:

          but it is most likely the d

          Not so very likely... The variable d is a DateTime structure, and can't be null.

          --- single minded; short sighted; long gone;

          E 1 Reply Last reply
          0
          • G Guffa

            Ennis Ray Lynch, Jr. wrote:

            but it is most likely the d

            Not so very likely... The variable d is a DateTime structure, and can't be null.

            --- single minded; short sighted; long gone;

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #5

            Since daten is not defined in could very well be a non-generic 1.1 .NET collection such as an array list which are collections of objects. Iterating on such a structure would attempt to assign null giving the null reference exception. Below is written in 2.0 but uses the 1.1 collection ArrayList. ArrayList list = new ArrayList(); list.Add(DateTime.Now); list.Add(null); foreach(DateTime d in list) { MessageBox.Show(d.ToShortDateString()); }


            File Not Found

            G 1 Reply Last reply
            0
            • E Ennis Ray Lynch Jr

              Since daten is not defined in could very well be a non-generic 1.1 .NET collection such as an array list which are collections of objects. Iterating on such a structure would attempt to assign null giving the null reference exception. Below is written in 2.0 but uses the 1.1 collection ArrayList. ArrayList list = new ArrayList(); list.Add(DateTime.Now); list.Add(null); foreach(DateTime d in list) { MessageBox.Show(d.ToShortDateString()); }


              File Not Found

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              No, it couldn't. If the daten collection would contain something other than DateTime values, the error would appear when the value is put in the d variable, not when the d variable is later used.

              --- single minded; short sighted; long gone;

              E 1 Reply Last reply
              0
              • G Guffa

                No, it couldn't. If the daten collection would contain something other than DateTime values, the error would appear when the value is put in the d variable, not when the d variable is later used.

                --- single minded; short sighted; long gone;

                E Offline
                E Offline
                Ennis Ray Lynch Jr
                wrote on last edited by
                #7

                Perhaps but when a question is posed so vague as to say where is my null reference exception i think any little bit of help will help. Now you are correct in that when I said d.Month I was wrong. Sometimes I say the stupidest things. Of course, that could also fall into the category of someone designing an OO language that doesn't fully support nulls until the third release.


                File Not Found

                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