Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Object reference not set to an instance of an object.

86 views
Skip to first unread message

Mitchell Freeman

unread,
Dec 5, 2002, 9:17:52 PM12/5/02
to
I am using the standard Rich Text Box control and am continually
feeding text to it as it is monitoring the activity of an application
I am running. At various points, the program I wrote will crash with
the exception below.

I have put Try/Catch statments in various places that have to do with
the RichTextBox and adding additional text (throug the AppendText
method), but that does not seem to have allowed me to prevent the
failure.

What my app basically does is take in data from the serial port, parse
it, send it over the internet (encrypted), wait for a response (also
encrypted), decrypt the response and send the decrypted response back
over the serial port. Since MS left Serial support out of VS.NET 2002,
I am using the registered AxMscomm controls from VS 6. (So far, I have
had no problems with the serial stuff working this way though I would
prefer a much better solution.)

In any event, after a fresh reboot of my system, it takes a good, long
time for this app to crash with the error below. After subsequent
Quit/Restarts of the app witout fresh reboots, this exception occurs
sooner and sooner. Since this is not directing me to an offending line
in my code, can anyone suggest where I should look to catch this
little bug and kill it?

Better yet, is there a relatively simple way of automatically
scrolling off older text in a RichTextBox while appending new text, or
in a separate operation? Is the RTB control smart enough to be told
"When you reach this much text, start dumping the stuff at the top to
make room for the stuff appended to the bottom"? Or is the error below
not really due to the maxing of the RTB at all in the first place and
I need to look somewhere else?

I have already tried setting MaxLength to a very small number and know
that is not the help I am looking for, as users will never type in
this window themselves. This is just a display window.

============================Start of
Exception===============================
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance
of an object.
at System.Windows.Forms.RichTextBox.EditStreamProc(IntPtr dwCookie,
IntPtr buf, Int32 cb, Int32& transferred)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.RichTextBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.3300.0
Win32 Version: 1.0.3705.288
CodeBase: file:///c:/winnt/microsoft.net/framework/v1.0.3705/mscorlib.dll
----------------------------------------
MyApp
Assembly Version: 1.0.1069.30300
Win32 Version: 1.0.1069.30300
CodeBase: file:blah blah blah
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.3300.0
Win32 Version: 1.0.3705.288
CodeBase: file:///c:/winnt/assembly/gac/system.windows.forms/1.0.3300.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.3300.0
Win32 Version: 1.0.3705.288
CodeBase: file:///c:/winnt/assembly/gac/system/1.0.3300.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.3300.0
Win32 Version: 1.0.3705.288
CodeBase: file:///c:/winnt/assembly/gac/system.drawing/1.0.3300.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
AxInterop.MSCommLib
Assembly Version: 1.1.0.0
Win32 Version: 1.1.0.0
CodeBase: file:blah blah blah
----------------------------------------
System.Xml
Assembly Version: 1.0.3300.0
Win32 Version: 1.0.3705.288
CodeBase: file:///c:/winnt/assembly/gac/system.xml/1.0.3300.0__b77a5c561934e089/system.xml.dll
----------------------------------------
Interop.MSCommLib
Assembly Version: 1.1.0.0
Win32 Version: 1.1.0.0
CodeBase: file:blah blah blah
----------------------------------------
Accessibility
Assembly Version: 1.0.3300.0
Win32 Version: 1.0.3705.0
CodeBase: file:///c:/winnt/assembly/gac/accessibility/1.0.3300.0__b03f5f7f11d50a3a/accessibility.dll
----------------------------------------

************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file for this
application or machine (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.
==============================End of
Exception=================================

Evan Freeman

unread,
Dec 5, 2002, 11:39:13 PM12/5/02
to
Hey Mitchell,

Looking at the error you get I'm going to take a guess here. What it looks
like is that you are doing is trying to use a null reference TRANSLATION: A
Null pointer, where this is happening in your code I couldn't say since I
can't see your code. What I would do is try adding in the Diagnostic name
space and using the Debug class to allert you when you end up with a NULL
reference. Something like the following:

{
Debug.Assert(MyRef, "I have a null where I shouldn't");
}

This will throw up a dialog and cause your app to break on the offending
line.

I would say sprinkle this through your code where you suspect the problems
might be happening, then run your debug build and wait.

What my guess is, is that the Garbage Collector might be cleaning up a
reference or handle on you. THough this is a guess and I can't be certain of
that. Either way using the asserts will probably help you track down the
problem. As to cleaning out the top of the edit control. you can do that
manually by saying you will only allow say 1000 characters to be written to
it then writing a function to do all the work. Sudo code:

I'm about to write some new stuff to the edit control lets check for how
much stuff we are going to writ.
if I i have 200 characters to write, and the edit control has 1000
characters in it
remove the characters from index 0 - 200 from the edit control then
append the new 200 characters.

Think of the edit control as a big character array or vector that does the
work of shrinking itself for you. Should do the trick.

Hope any of this is helpful.

-Evan


"Mitchell Freeman" <mitc...@winhack.com> wrote in message
news:3620c398.02120...@posting.google.com...

Mitchell Freeman

unread,
Dec 6, 2002, 10:59:51 AM12/6/02
to
Thanx for the info. I did some additional testing last night and
determined that this is all about that RichTextBox. What I did was
drastically reduce the amount of text I was writing to it, and the
program lived for a significantly longer time. I was unable to get it
to crash after several hours when I was writing 20+/- chars to the RTB
than the hundreds I was writing before (when writing the large amount
of text before, it would usually crash within an hour or less per
run). Still, this does not solve my problem, but at least proves that
the problem is related to the RTB and not to something else in my code
at least. I could always just not write the monitoring text to the RTB
at all and let the log file handle it, but that defeats the purpose of
having a visual monitoring app in the first place.

Because the exception is breaking on some of the internal calls inside
what C# is doing with Win32 and because the exception is not
specifying a line in MY code, I have to presume that the error (null
reference) is something MS is doing to themselves and not something I
am doing (directly), though I do understand that it is what I am doing
to/with the RTB that is bringing on this error in the first place.

I will take your suggestion and put that Debug.Assert statement in my
code. Do I just plop that line any old place, or does it have to be in
specific spots to work properly? I'll have to research that a little,
since I have never used it before (that I can recall, anyway).

Lastly, on managing the text going into the RTB and clearing out old,
the pesudo code you gave me is understandable, but in looking at the
members for RichTextBox, as well as TextBox and TextBoxBase, I did not
see any functions for clearing portions of the text, either by
index,length or by # of lines or anything. I only saw a Clear function
(which drops all the text), and a Select-then-Cut function pair (which
gets it out of the RTB, but stuffs the cut text onto the clipboard,
which is not really what I want to do). Is there a method there for
clearing text that I am missing?

Thanx for the help and the clues thus far.


"Evan Freeman" <Eva...@rcn.com> wrote in message news:<7LVH9.92$O3...@news-srv1.fmr.com>...

Martin Dew

unread,
Dec 6, 2002, 11:24:11 AM12/6/02
to

"Mitchell Freeman" <mitc...@winhack.com> wrote in message
news:3620c398.0212...@posting.google.com...

Purely as a passing thought, In my monitoring apps I normally use either a
lstbox, or listview component for the informational display, then you can
use the count property and recycle your oldest with newest after a desired
level, ie when it reaches 1000, loose the first one each time and add a new
one to the end. This seems to work well for me.

Martin


Evan Freeman

unread,
Dec 6, 2002, 4:58:03 PM12/6/02
to
Mitchell,

I would agree with Martin on the List stuff. I also apologize for my sudo
code, I do so much work in C++ .NET mixing managed and unmanaged code, do to
the fact that I find it more robust than C#, not easier, but gives me more
flexability that I like and need, that I forgot that you can't treat the
control like that. Martin has a much better solution than me for that.

But, the only way to do it with the text control would be to copy all your
text into a string, manipulate the string then overright the text in your
control.

As to the Debug class, you can plop it any where you like.

Also look for my article on how to use that class in an upcoming issue of
Windows Developer Magazine =] ... little plug for myself!!

- Evan


"Martin Dew" <Marti...@adastra.co.uk> wrote in message
news:#POQmPUnCHA.1224@TK2MSFTNGP08...

0 new messages