472,954 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

[MC++] NullReferenceException with value structs?

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])
{ }
};

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 12 '06 #1
3 1215
Marcus Kwok <ri******@gehennom.invalid> wrote:
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.

ContoursDialog::Contours contours; // HERE I INSTANTIATE
write_contours_file(contours); // HERE I CALL

// 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);


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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 16 '06 #2
Marcus Kwok <ri******@gehennom.invalid> wrote:
Marcus Kwok <ri******@gehennom.invalid> wrote:
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.

ContoursDialog::Contours contours; // HERE I INSTANTIATE
write_contours_file(contours); // HERE I CALL

// 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);


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);


Nevermind, I still get the crash...

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 16 '06 #3
Marcus Kwok <ri******@gehennom.invalid> wrote:
Marcus Kwok <ri******@gehennom.invalid> wrote:
Marcus Kwok <ri******@gehennom.invalid> wrote:
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.

ContoursDialog::Contours contours; // HERE I INSTANTIATE
write_contours_file(contours); // HERE I CALL

// 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);


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);


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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
3
by: Keith M | last post by:
Hi, I am a bit of a newcomer to C# but have experience with (unmanaged) C++. Now, I have a 3rd party dll and headers. This dll is a C++ style dll that exports classes and structs. I am...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
1
by: Gurminder | last post by:
Hi, I am new to C++ & MC++. I am trying to write a wrapper class for exsiting code which uses libraries(Adobe Framemaker) to open documents. These libraries are written in C++. The code is :-...
1
by: Shawn B. | last post by:
Greetings, With a Managed class, if I'm #including a Windows SDK header file, and call an API, it appears (according to .NET Reflector) that it automatically generates a for each API call that...
1
by: Marcus Kwok | last post by:
I am not sure if this is related to my previous thread ("NullReferenceException with value struct") so I am starting a new thread. I saw in...
1
by: razilon | last post by:
Hi, I've written a managed class that makes use of stl vectors of a few unmanaged structs for data handling/manipulation, but I'm getting a few very strange errors. I get an "Unhandled...
0
by: razilon | last post by:
Hi, I've written a managed class that makes use of stl vectors of a few unmanaged structs for data handling/manipulation, but I'm getting a few very strange errors. I get an "Unhandled...
3
by: Sagar | last post by:
Hi. I am working on a project to migrate a web application from 1.1 to 2.0 Within in the DAL of the application, there is a call to below function that builds a command object for later use. ...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.