[RESOLVED] DataGridViewPrinter NullReferenceException
I get this error message: Object reference not set to an instance of an object
at the bolded line of this code:
Code:
Public Sub MyPrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles myprintdocument.PrintPage
Dim more As New Boolean
more = mydatagridviewprinter.DrawDataGridView(e.Graphics)
If more Then e.HasMorePages = True
End Sub
Here is DrawDataGridView:
Code:
Public Function DrawDataGridView(ByRef g As Graphics) As Boolean
Try
Calculate(g)
DrawHeader(g)
Dim bContinue As Boolean = DrawRows(g)
Return bContinue
Catch ex As Exception
MessageBox.Show("Operation failed: " + ex.Message.ToString(), Application.ProductName + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
Any help solving this would be greatly appreciated. Thanks.
Re: DataGridViewPrinter NullReferenceException
There are only two references on that line that could cause that error and we know that 'e' is not Nothing so it can only be that mydatagridviewprinter is Nothing. NullReferenceExceptions are the easiest exceptions to diagnose. It's pretty simple to look at the line that threw the exception and find out which reference is Nothing. It's often pretty easy to trace back through the code to find the place where you expect that reference to be set too. I'm guessing that this is one of those situations.
Re: DataGridViewPrinter NullReferenceException