locked
Visual C++ Error An unhandled exception of type 'System.NullReferenceException' occurred in System.Drawing.dll Additional information: Object reference not set to an instance of an object. RRS feed

  • Question

  • The above error occurs when I try to print the contents of a standard TextBox. Shown below the code, the ** indicates where the error occurs.

     void pd_PrintPage( Object^ sender, PrintPageEventArgs^ ev )
       {
     
     int lineCount = 0;
     int charactersOnPage = 0;
          int linesPerPage = 0;
     mStr = txtEditor->Text; 
     charCount = txtEditor->Text->Length;
     
     mFont = gcnew System::Drawing::Font("New Courier", 10);
    **  ev->Graphics->MeasureString(mStr, mFont, ev->MarginBounds.Size, StringFormat::GenericTypographic, charactersOnPage, linesPerPage);**
     ev->Graphics->DrawString(mStr, mFont, Brushes::Black, ev->MarginBounds, StringFormat::GenericTypographic);

          String^ line = "";
          // Calculate the number of lines per page.
          linesPerPage = ev->MarginBounds.Height / mFont->GetHeight( ev->Graphics );
           mStr = mStr->Substring(charactersOnPage);
          // If more lines exist, print another page.
          if ( line != nullptr )
                ev->HasMorePages = true;
          else
                ev->HasMorePages = false;
       }
    This same code (operators excepted) works great on both Visual C# 2008 and Visual Basic 2008
    Thursday, August 27, 2009 8:57 PM

Answers

  • I don't see it.  The code will run forever though, your HasMorePages logic is flawed.  "line" will never be a nullptr.  Something is going to go kaboom sooner or later.

    Hans Passant.
    Well, I found the problem. It just seems sometimes I fall into that catagory of programmers that "Can't see the Forest for the Trees!" I did correct the line="" to line=nullptr, that was just a typo error. The real problem was I placed the print code in the wrong method. Instead of placing it into the PrintDocument1_PrintPage(Object^ sender, PrintPageEventArgs^ ev), I created my own stupid mentod pd_PrintPage(Object^ sender, PrintPageEventArgs^ ev) and called that method which did not work. What was confusing was that the error occured during 'run' not during the 'Build Solution'.  Anyway thanks for the response, it got my brain channeled in a different direction.
    ____________________________________

    Tom Barnett
    • Marked as answer by Kira Qian Thursday, September 3, 2009 8:02 AM
    Friday, August 28, 2009 3:17 PM
  • Well the reason it compiled is that the code is syntactically right. But the event args will be null, and thus at runtime you get the exception.
    http://blog.voidnish.com
    • Marked as answer by Kira Qian Thursday, September 3, 2009 8:02 AM
    Friday, August 28, 2009 3:48 PM

All replies

  • Put a break point there and see which object is null there.
    http://blog.voidnish.com
    Thursday, August 27, 2009 9:06 PM
  • I don't see it.  The code will run forever though, your HasMorePages logic is flawed.  "line" will never be a nullptr.  Something is going to go kaboom sooner or later.

    Hans Passant.
    Thursday, August 27, 2009 9:49 PM
  • I don't see it.  The code will run forever though, your HasMorePages logic is flawed.  "line" will never be a nullptr.  Something is going to go kaboom sooner or later.

    Hans Passant.
    Well, I found the problem. It just seems sometimes I fall into that catagory of programmers that "Can't see the Forest for the Trees!" I did correct the line="" to line=nullptr, that was just a typo error. The real problem was I placed the print code in the wrong method. Instead of placing it into the PrintDocument1_PrintPage(Object^ sender, PrintPageEventArgs^ ev), I created my own stupid mentod pd_PrintPage(Object^ sender, PrintPageEventArgs^ ev) and called that method which did not work. What was confusing was that the error occured during 'run' not during the 'Build Solution'.  Anyway thanks for the response, it got my brain channeled in a different direction.
    ____________________________________

    Tom Barnett
    • Marked as answer by Kira Qian Thursday, September 3, 2009 8:02 AM
    Friday, August 28, 2009 3:17 PM
  • Well the reason it compiled is that the code is syntactically right. But the event args will be null, and thus at runtime you get the exception.
    http://blog.voidnish.com
    • Marked as answer by Kira Qian Thursday, September 3, 2009 8:02 AM
    Friday, August 28, 2009 3:48 PM
  • JD: "If there's a problem, usually it's resolved itself in some way. By then, you know, it's very much an internal kind of working, a machine."

    GLE: "This process involves thinking?"

    JD: "Yes. . ."

    William Shakespeare "THE QUEEN'S MEN" :)
    _____________________________________

    Tom Barnett
    Friday, August 28, 2009 6:48 PM