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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. NullReferenceException with delegate function.

NullReferenceException with delegate function.

Scheduled Pinned Locked Moved C#
regexcsharpvisual-studiosysadminhelp
5 Posts 2 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.
  • T Offline
    T Offline
    Teuz
    wrote on last edited by
    #1

    Hi guys, I'm starting out with C# and since I dont like messing with simple things (:D) I'm already "playing" with threads and delegates. In my program I create a net socket, connect to a server and start to receive data async with BeginReceive. When a message is received, the program(secondary thread of BeginReceive) pass it to RlsInsert(message) that split it with a regular expression and populate a listview. It seems to work perfectly but randomly VS pops up with an exception about NullReferenceException and tells me to use the "new" keyword, but as you can see I'm already using it: //Add item to the list view public delegate void RlsInsertCallback(string text); public void RlsInsert(string text) { if (this.InvokeRequired) this.Invoke(new RlsInsertCallback(this.RlsInsert),new object[] { text }); <--- expection on this line pointin to object[] else { **more code that check text with regular expression, split text and populate subitems etc lvRelease.Items.Add(aItem); } } Can anybody tell me what I am doing wrong? Because I cant figure it out. It works and then It crashes after a while. Sorry if this has already been asked, I did used search but I could not find anything that really match my problem. Thank you in advance. Matteo

    C 1 Reply Last reply
    0
    • T Teuz

      Hi guys, I'm starting out with C# and since I dont like messing with simple things (:D) I'm already "playing" with threads and delegates. In my program I create a net socket, connect to a server and start to receive data async with BeginReceive. When a message is received, the program(secondary thread of BeginReceive) pass it to RlsInsert(message) that split it with a regular expression and populate a listview. It seems to work perfectly but randomly VS pops up with an exception about NullReferenceException and tells me to use the "new" keyword, but as you can see I'm already using it: //Add item to the list view public delegate void RlsInsertCallback(string text); public void RlsInsert(string text) { if (this.InvokeRequired) this.Invoke(new RlsInsertCallback(this.RlsInsert),new object[] { text }); <--- expection on this line pointin to object[] else { **more code that check text with regular expression, split text and populate subitems etc lvRelease.Items.Add(aItem); } } Can anybody tell me what I am doing wrong? Because I cant figure it out. It works and then It crashes after a while. Sorry if this has already been asked, I did used search but I could not find anything that really match my problem. Thank you in advance. Matteo

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

      Could this.RisInsert or text be null ?

      Christian Graus Driven to the arms of OSX by Vista.

      T 1 Reply Last reply
      0
      • C Christian Graus

        Could this.RisInsert or text be null ?

        Christian Graus Driven to the arms of OSX by Vista.

        T Offline
        T Offline
        Teuz
        wrote on last edited by
        #3

        Thanks for replying. I changed the code a bit includin a check for null text: public void RlsInsert(string text) { if (text != null) { if (this.InvokeRequired) this.Invoke(new RlsInsertCallback(ListAddItem), new object[] { text }); else { ListAddItem(text); } } } But I get the same error. Not sure about this.RlsInsert (ListAddItem: I put the code to populate the listview in another method) null or not. How can it be null?

        C 1 Reply Last reply
        0
        • T Teuz

          Thanks for replying. I changed the code a bit includin a check for null text: public void RlsInsert(string text) { if (text != null) { if (this.InvokeRequired) this.Invoke(new RlsInsertCallback(ListAddItem), new object[] { text }); else { ListAddItem(text); } } } But I get the same error. Not sure about this.RlsInsert (ListAddItem: I put the code to populate the listview in another method) null or not. How can it be null?

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

          I would do a try/catch and then check the values of the various variables when the catch occurs.

          Christian Graus Driven to the arms of OSX by Vista.

          T 1 Reply Last reply
          0
          • C Christian Graus

            I would do a try/catch and then check the values of the various variables when the catch occurs.

            Christian Graus Driven to the arms of OSX by Vista.

            T Offline
            T Offline
            Teuz
            wrote on last edited by
            #5

            Ok I'm doing it right now, I'll let you know. Ty

            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