1

Working on a WinForms app in C# with a DataGridView control. I am running into an issue with an Exception Handle. I am working with a grid view and when it finishes with 1 row, and moves on to the next, it throws this exception and the view gets a red X across it. What is particularly interesting is that even though the exception is being thrown, it still completes appropriately.

I am having difficulty diagnosing where this error is occurring.

Here is the code for the Exception Handler:

static void GlobalThreadExceptionHandler(object sender, ThreadExceptionEventArgs args)
{
    Exception e = (Exception)args.Exception;
    StopWatch.Logging.Logger.Instance.Log("Unhandled thread exception.", StopWatch.Logging.Logger.EntryType.Crash, e);
}

Here is the Exception message:

"Object reference not set to an instance of an object."

Here is the output for the error:

£: 2021-11-16 11:55:11 AM CRASH Unhandled thread exception.
   at System.Windows.Forms.DataGridViewButtonCell.PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates elementState, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, Boolean computeContentBounds, Boolean computeErrorIconBounds, Boolean paint)
   at System.Windows.Forms.DataGridViewButtonCell.Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates elementState, Object value, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
   at System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
   at System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
   at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
   at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
   at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
   at System.Windows.Forms.DataGridView.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.DataGridView.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)

I do not have a lot of experience with WinForms, or Threads, so I am at a loss on how to find out where this Object Reference error is coming from, so that I can fix it. My attempts at using breakpoints to find where this exception is coming from have not revealed where it is coming from.

Thanks in advance!

KorBee
  • 11
  • 2
  • @JohnG he states that doesn't know where in his code this error is coming from. The call stack shows only WinForms internal functions. – user3147973 Nov 16 '21 at 20:14
  • 2
    Try disabling your global exception handler and reproduce the crash again. You might get a different call stack. – user3147973 Nov 16 '21 at 20:19

0 Answers0