NullReferenceException with delegate function.
-
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
-
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
Could this.RisInsert or text be null ?
Christian Graus Driven to the arms of OSX by Vista.
-
Could this.RisInsert or text be null ?
Christian Graus Driven to the arms of OSX by Vista.
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?
-
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?
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.
-
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.