Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2006

Tip: Looking for answers? Try searching our database.

[MC++] NullReferenceException with value structs?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marcus Kwok - 12 May 2006 20:45 GMT
I have been staring at this code and I cannot see what is wrong with it.
I am getting a NullReferenceException when trying to pass a value struct
to a method.  This exception only happens in Release mode; Debug mode
runs fine.  When I use the Debugger on Release mode, the debugger shows
valid values for "contour", but I still get this exception.  If anybody
has any input at all, it would be much appreciated.

---------------------------
System.NullReferenceException: Object reference not set to an instance of an object.

  at FSBSGui2.ControlPanel.write_contours_file(Contours& contours)

  at FSBSGui2.ControlPanel.button_run_Click(Object sender, EventArgs e)
---------------------------

System::Void button_run_Click(System::Object *  sender, System::EventArgs *  e)
{
   using System::Text::StringBuilder;
   using System::Threading::Thread;
   using System::Threading::ThreadStart;
   using System::Windows::Forms::Button;
   using System::Windows::Forms::DialogResult;
   using System::Windows::Forms::MessageBox;
   using System::Windows::Forms::MessageBoxButtons;
   using System::Windows::Forms::MessageBoxIcon;

   Button* button = __try_cast<Button*>(sender);
   if (button->Text->Equals(S"&Cancel")) {
       button_cancel_Click(sender, e);
       return;
   }

   fill_FSBSParameters();
   ContoursDialog::Contours contours;    // HERE I INSTANTIATE
   switch (run_type) {
   case Xmtr_Avl:
       contours = xmtr_avail_contours;
       break;
   case Fer:
       contours = fer_contours;
       break;
   case Pcmr:
       contours = pcmr_contours;
       break;
   default:
       break;
   }

   VerificationDialog* vd = new VerificationDialog(*params, contours);
   if (vd->ShowDialog() == DialogResult::OK) {
       try {
           write_contours_file(contours);    // HERE I CALL
       }
       catch (System::NullReferenceException* e) {
           System::Windows::Forms::MessageBox::Show(e->ToString());
       }
   }
}

// Here is the prototype for write_contours_file()
// I notice that the 'const' has been dropped in the stack trace above
System::Void write_contours_file(const ContoursDialog::Contours& contours);

// Here is the definition of ContoursDialog::Contours
__value struct Contours {
   int num_contours;

   __value struct ContourInfo {
       double lower_bound;
       int r;
       int g;
       int b;
   };

   ContourInfo contours[];

   Contours()
       : contours(new ContourInfo[max_contours])
   { }
};

Signature

Marcus Kwok
Replace 'invalid' with 'net' to reply

Marcus Kwok - 16 May 2006 14:53 GMT
> I have been staring at this code and I cannot see what is wrong with it.
> I am getting a NullReferenceException when trying to pass a value struct
[quoted text clipped - 7 lines]
> // I notice that the 'const' has been dropped in the stack trace above
> System::Void write_contours_file(const ContoursDialog::Contours& contours);

Well, I managed to work around it by changing it to

   System::Void write_contours_file(const ContoursDialog::Contours* contours);
   ...
   ContoursDialog::Contours contours;
   write_contours_file(&contours);

I am just confused why I had to do this.  I even made an intermediary
function:

   System::Void print_contours(const ContoursDialog::Contours& contours);

which would print the contours and then call
write_contours_file(contours).  The print_contours() function worked
perfectly fine, but then it still failed on the call to
write_contours_file(), even though both functions received their
parameters in exactly the same way.

Signature

Marcus Kwok
Replace 'invalid' with 'net' to reply

Marcus Kwok - 16 May 2006 15:33 GMT
>> I have been staring at this code and I cannot see what is wrong with it.
>> I am getting a NullReferenceException when trying to pass a value struct
[quoted text clipped - 14 lines]
>    ContoursDialog::Contours contours;
>    write_contours_file(&contours);

Nevermind, I still get the crash...

Signature

Marcus Kwok
Replace 'invalid' with 'net' to reply

Marcus Kwok - 16 May 2006 20:18 GMT
>>> I have been staring at this code and I cannot see what is wrong with it.
>>> I am getting a NullReferenceException when trying to pass a value struct
[quoted text clipped - 16 lines]
>
> Nevermind, I still get the crash...

OK, I think I solved it for real this time.

In my write_contours_file() function, I had the line:

   // Here, line is a System::Text::StringBuilder*
   line->Append(__box(std::log10(lower_bound))->ToString());

I changed it by adding a temporary variable:

   double temp = std::log10(lower_bound);
   line->Append(__box(temp)->ToString());

I still fail to see why the error would only occur in Release mode, and
why the debugger would crash where I called the function
write_contours_file(), instead of letting me step through the function
so I could have saved myself 3 days working on this.

Signature

Marcus Kwok
Replace 'invalid' with 'net' to reply


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.