By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,516 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

inexplicable System.NullReferenceException when using stl vector in managed class

P: n/a

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
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;
// 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(),
//Unhandled Exception: System.NullReferenceException: Object
reference not set to an instance of an object
lastWasHeadline = false;

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


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

bounds.left = bounds.right = = bounds.bottom = 0;
adjacent.left = adjacent.right = = adjacent.bottom =
gutterDist.left = gutterDist.right = =
gutterDist.bottom = 2147483647;
type = blockType::undecided;

~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.bottom = newParagraph.bounds.bottom;
if (newParagraph.bounds.left < bounds.left)
bounds.left = newParagraph.bounds.left;
if (newParagraph.bounds.right > bounds.right)
bounds.right = newParagraph.bounds.right;
if ( < =;
if (newParagraph.bounds.bottom > bounds.bottom)
bounds.bottom = newParagraph.bounds.bottom;

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

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

width = height = resolution = 0;

~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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.