473,398 Members | 2,188 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,398 software developers and data experts.

inexplicable System.NullReferenceException when using stl vector in managed class

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 Exception: System.NullReferenceException: Object reference
not set to an instance of an object" occasionally when adding a new
element to a vector. By

occasionally I mean that the exact same code works fine most of the
time, throwing the error around 1% of the time. Based on sample data
that I'm feeding the program, the

errors occur on the exact same data every execution, but I don't see
anything wrong with the offending data or the code. Can someone help?
Even if you only have a vague

idea of something I can look into, please let me know.

Here's the relevant code snippet:

// if a headline occurs after a normal paragraph, split the block
there
for (int i = thisDoc->blocks.size() - 1; i >= 0; i--)
{
block* curBlock = &thisDoc->blocks[i];
if (curBlock->type == blockType::text)
{
// search through all the paragraphs (from end to start)
bool lastWasHeadline = false;
for (int j = curBlock->paragraphs.size() - 1; j >= 0; j--)
{
// is this paragraph a headline?
if (curBlock->paragraphs[j].type == paragraphType::headline)
lastWasHeadline = true;
else
{
// did we just leave a headline?
if (lastWasHeadline)
{
// split the remainder of the block into a new block
block newBlock;
newBlock.type = blockType::text;
for (int k = curBlock->paragraphs.size() - 1; k > j; k--)
{
newBlock.paragraphs.insert(newBlock.paragraphs.beg in(),
curBlock->paragraphs[k]);
curBlock->removeParagraph(k);
}
//!!!!!!!!!!!!!!!!!!ERROR!!!!!!!!!!!!!!!!!!
//Unhandled Exception: System.NullReferenceException: Object
reference not set to an instance of an object
thisDoc->addBlock(newBlock);
//!!!!!!!!!!!!!!!!!!ERROR!!!!!!!!!!!!!!!!!!
}
lastWasHeadline = false;
}
}
}
}

From the class header (to show you what my struct declarations look

like):

__nogc struct block
{
rectangle bounds;
rectangle adjacent;
rectangle gutterDist;
int type;
vector<paragraph> paragraphs;

block()
{
bounds.left = bounds.right = bounds.top = bounds.bottom = 0;
adjacent.left = adjacent.right = adjacent.top = adjacent.bottom =
-1;
gutterDist.left = gutterDist.right = gutterDist.top =
gutterDist.bottom = 2147483647;
type = blockType::undecided;
paragraphs.clear();
}

~block() {paragraphs.clear();}

void addParagraph(paragraph newParagraph)
{
// if this is the first line to be added to the paragraph
if (paragraphs.size() == 0)
{
bounds.left = newParagraph.bounds.left;
bounds.right = newParagraph.bounds.right;
bounds.top = newParagraph.bounds.top;
bounds.bottom = newParagraph.bounds.bottom;
}
else
{
if (newParagraph.bounds.left < bounds.left)
bounds.left = newParagraph.bounds.left;
if (newParagraph.bounds.right > bounds.right)
bounds.right = newParagraph.bounds.right;
if (newParagraph.bounds.top < bounds.top)
bounds.top = newParagraph.bounds.top;
if (newParagraph.bounds.bottom > bounds.bottom)
bounds.bottom = newParagraph.bounds.bottom;
}
paragraphs.push_back(newParagraph);
}

void removeParagraph(int index)
{
paragraphs.erase(paragraphs.begin() + index);
}
};

__nogc struct page
{
int width, height, resolution;
vector<block> blocks;
double avgLineHeight; // calculated in reFormat(...)

page()
{
width = height = resolution = 0;
blocks.clear();
}

~page() {blocks.clear();}

void addBlock(block newBlock) {blocks.push_back(newBlock);}

void removeBlock(int index)
{
blocks.erase(blocks.begin() + index);
}
};

// all necessary internal variables
page __nogc* thisDoc;

May 25 '06 #1
0 1313

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...
0
by: Yoni Rabinovitch | last post by:
Hi, I am new to C#, so I apologise if this is a stupid question. I have some 3rd party C++ code, which gets built as static libraries (not DLLs). I want to create a C# form, which will call...
11
by: Andreas Wirén | last post by:
Hi I'm a C# .NET newbie doing a minor school project but I'm getting a strange error message. 'System.NullReferenceException' occurred in system.windows.forms.dll Additional information: Object...
1
by: Klynt | last post by:
Project built using /CLR, but code is old and has not been converted specifically to "managed" (__gc or __value). Everything seemed to work great, until I got the following error. I have a...
5
by: AAguiar | last post by:
I have an asp.net project where the code behind the aspx page calls a c# class which makes calls to a managed static C++ class. The C# class works fine when the asp net worker process starts, when...
8
by: johnmmcparland | last post by:
Hi all, my program is trying to add group boxes with radio buttons at run time. While at one point it was able to draw the group boxes without the radio buttons, now it encounters problems just...
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...
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...
1
by: Bruce | last post by:
I am using VC .NET 2003. I created a simple unmanaged class in C++. The class is contained in a library. I then created a simple managed class wrapper that calls a function in the unmanaged...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.