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.NullRefe renceException: 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.siz e() - 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 (lastWasHeadlin e)
{
// split the remainder of the block into a new block
block newBlock;
newBlock.type = blockType::text ;
for (int k = curBlock->paragraphs.siz e() - 1; k > j; k--)
{
newBlock.paragr aphs.insert(new Block.paragraph s.begin(),
curBlock->paragraphs[k]);
curBlock->removeParagrap h(k);
}
//!!!!!!!!!!!!!!! !!!ERROR!!!!!!! !!!!!!!!!!!
//Unhandled Exception: System.NullRefe renceException: Object
reference not set to an instance of an object
thisDoc->addBlock(newBl ock);
//!!!!!!!!!!!!!!! !!!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<paragrap h> paragraphs;
block()
{
bounds.left = bounds.right = bounds.top = bounds.bottom = 0;
adjacent.left = adjacent.right = adjacent.top = adjacent.bottom =
-1;
gutterDist.left = gutterDist.righ t = gutterDist.top =
gutterDist.bott om = 2147483647;
type = blockType::unde cided;
paragraphs.clea r();
}
~block() {paragraphs.cle ar();}
void addParagraph(pa ragraph newParagraph)
{
// if this is the first line to be added to the paragraph
if (paragraphs.siz e() == 0)
{
bounds.left = newParagraph.bo unds.left;
bounds.right = newParagraph.bo unds.right;
bounds.top = newParagraph.bo unds.top;
bounds.bottom = newParagraph.bo unds.bottom;
}
else
{
if (newParagraph.b ounds.left < bounds.left)
bounds.left = newParagraph.bo unds.left;
if (newParagraph.b ounds.right > bounds.right)
bounds.right = newParagraph.bo unds.right;
if (newParagraph.b ounds.top < bounds.top)
bounds.top = newParagraph.bo unds.top;
if (newParagraph.b ounds.bottom > bounds.bottom)
bounds.bottom = newParagraph.bo unds.bottom;
}
paragraphs.push _back(newParagr aph);
}
void removeParagraph (int index)
{
paragraphs.eras e(paragraphs.be gin() + 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_ba ck(newBlock);}
void removeBlock(int index)
{
blocks.erase(bl ocks.begin() + index);
}
};
// all necessary internal variables
page __nogc* thisDoc;