473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1236
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. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.