0

I wrote a data acquisition software that stores the data in several tables and plots this data using the Forms.DataVisualization.Charting.Chart object. To point out special events I use StripLines. The software uses the MyApplication_UnhandledException event to catch all unhandled exception an saves the exception information to a log file.

Unfortunately I got a "NullReferenceException" that seems to have been thrown within a framework method:

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.DataVisualization.Charting.StripLine.PaintTitle(ChartGraphics graph, PointF point1, PointF point2)
   at System.Windows.Forms.DataVisualization.Charting.StripLine.Paint(ChartGraphics graph, CommonElements common, Boolean drawLinesOnly)
   at System.Windows.Forms.DataVisualization.Charting.Axis.PaintStrips(ChartGraphics graph, Boolean selectionMode, Int32 x, Int32 y, Object& obj, Boolean drawLinesOnly)
   at System.Windows.Forms.DataVisualization.Charting.Axis.PaintStrips(ChartGraphics graph, Boolean drawLinesOnly)
   at System.Windows.Forms.DataVisualization.Charting.ChartArea.Paint(ChartGraphics graph)
   at System.Windows.Forms.DataVisualization.Charting.ChartPicture.Paint(Graphics graph, Boolean paintTopLevelElementOnly)
   at System.Windows.Forms.DataVisualization.Charting.Chart.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

In principle I know the cause of and how deal with a NullReferenceException, but in this special case I'm a bit at a loss since: - I've not idea which object I should check for being nothing - At what point in code i should do the check - Where to put a Try Catch block since all of the stack is not within my code.

In addition I should mention that the data acquisition runs in a Backgroundworker thread.

Unfortunately the exception is not reproducible / I do not know what that user had done before the exception had been thrown.

I know that these are not the best conditions for debugging but it would be great if some has an idea - what could have caused this exception (e.g. disposed Stripline or charting object) - and how to avoid or at least handle the exception at such a target site?

Thx, Marcus

Marcus
  • 1
  • 1
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Trevor Jul 18 '19 at 15:13
  • Depending on the settings, sometime you don't see the actual line of the error. Especially if you run threads or events. If it's really end up being inside the method, .NET is fully open source and you could look at what it is trying to do and it could help you figure out which parameter you pass is wrong. – the_lotus Jul 18 '19 at 15:16
  • It's possible that the StripLine's `Text` property is `Nothing`. Using ILSpy, I can see that the `PaintTitle` method does `if (Text.Length > 0)` and `Text`'s set accessor doesn't prevent `Nothing` from being assigned to it (although it's initialized to `""`). Make sure that whatever data you have that could be assigned to `Text` is not `Nothing`. – madreflection Jul 18 '19 at 15:22
  • 1
    I'm wondering: is it possible that the user deleted the chart or part of it while the backgroundworker was still doing work? Then when the backgroundworker completes it might try to force an update of the chart(element) which has been deleted in the meantime, thus causing the nullreference exception? – AConfusedSimpleton Jul 19 '19 at 08:40
  • 1
    @Çöđěxěŕ, imho this isn't really a duplicate; OP specifies that he knows what it is, but requires help debugging it within a framework method. – AConfusedSimpleton Jul 19 '19 at 08:56
  • @the_lotus I think Core is fully open source, but I don't think Framework is fully open source. A lot of the "reference" source is available, but there are parts that are not. – Craig Jul 19 '19 at 17:15
  • @AConfusedSimpleton The user can't delete the Graph but they can "hide" the StripLines using a checkbox in the GUI. But this check box does not immediately redraw the graph. All updates of the graph are initiated by a Forms.Timer (to avoid any calls from other then the GUI thread) and the corresponding data source are all locked by SyncLock while updating the chart. But since the exception was thrown within the Stripline PaintTitle method, I would assume that the StripLine still exists. Our could removing a StripLine from the collection cause the exception? – Marcus Jul 22 '19 at 13:09
  • @madreflection I've implemented your suggestion and have taken care that the Text property of the StripLines can't be nothing. Frankly speaking: Actually I didn't have it in mind, that String can be nothing. In everyday life I always assume String to be a value type and not a reference type. Unfortunately there won't be any easy test to see if this have solved the issue. – Marcus Jul 22 '19 at 13:17

0 Answers0