Can't locate source of error: Object reference not set to an instance of an object

  • Thread starter Thread starter Jim P.
  • Start date Start date
J

Jim P.

I have a TCPClient & TCPListener class that I need to stress test. Using a
GUI, I built a class that would connect and send consecutive messages for 10
seconds, and log the the results in a richTextBox. Problem is that it runs
for a couple seconds, then I get the following error, ending the program:

--------------------------------------
An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll
Additional information: Object reference not set to an instance of an object
--------------------------------------

It seems that it was quitting during a do/while loop that connects & sends
rapid fire 32k messages for 10 seconds. I figured that maybe the
TCPListener couldn't handle the number of request, but the Lsitener class is
designed to call an Error Handler is it can't take any more sockets. So I
used Thread.Sleep(100), and that helped a little. Somethimes it will work
other times it fails. But that doesn't help me locate the source of the
problem.

Visual Studio marks the Application.Run as the source of the problem.

------------------------------
static void Main()
{
Application.Run(new FormClient());
}
------------------------------

But I can't get any more information about the source of the error. So I
wrapped the Application.Run in a try catch block and I still get the above
message and no details. Any idea how I could debug this further? Or know
of any articles on how to use TCPClient to build a TCPListner tester by
sending mass quantities of messages simultaneously?

thanks
 
Hi Jim

Although you don't say, are you using multi-threading?

If so, bear in mind that the RichTextBox is single-threaded (like all
Windows.Forms controls) and any access to it must be marshalled to the
thread on which it was created, using the Invoke method.

Just a thought.

Charles
 
If you open and close the connection for each transaction. Maybe that is the
problem.

Windows don't allow more than 2000? (I'm not sure, but I think that is 2000)
simultaneus TCP sessions.
So, do a netstat -a in your command line.

If you have a lot of ...

TCP machine_name:3577 machine_name:port TIME_WAIT


That's your problem.

AA
 
I was getting the same error in C#. I fixed it by using the Invoke
command, like Charles mentioned earlier. I am updating a DataTable
which is a datasource of a DataGrid. I was just directly calling the
procedure that updated the DataTable. This was causing an exception
outside my code, like you are getting. It has something to do with
accessing a form component from your thread that doesn't "own" the
component.

Here is what fixed it for me:

Define the procedure that does the updating like this:
public void UpdGrid_handler(object sender, EventArgs evArgs)

Whenever you need to update the grid, use a line like this:
grid.Invoke(new EventHandler(UpdGrid_handler));

Hope this helps,
Mick
 
Back
Top