Results 1 to 3 of 3

Thread: [RESOLVED] DataGridViewPrinter NullReferenceException

  1. #1

    Thread Starter
    Addicted Member Spirited Machine's Avatar
    Join Date
    May 2009
    Posts
    215

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member Spirited Machine's Avatar
    Join Date
    May 2009
    Posts
    215

    Re: DataGridViewPrinter NullReferenceException

    Thanks Mr. McIlhinney.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width