473,387 Members | 1,745 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,387 software developers and data experts.

inexplicable System.NullReferenceException when using stl vector

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 24 '06 #1
1 2008
ra*****@hotmail.com wrote:
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

<snip>

The error does not seem to come from the standard features you are
using. It is perhaps because of this "managed" C++. Asking in a
microsoft newsgroup should help you (see
http://www.parashift.com/c++-faq-lit....html#faq-5.9).
Jonathan

May 24 '06 #2

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...
1
by: Rafael | last post by:
Hi, I hope I can find some help for this problem IDE: Visual Studio.NET 2003 Developer Editio Language: C# Problem: "An unhandled exception of type 'System.NullReferenceException' occurred in...
7
by: mike p. | last post by:
I have a docbook xml file, and am using standard docbook 1.61.3 xsl stylesheets to do xhtml transform. Transform works fine when using MSXML. When I try to do the following using asp.net 1.1: ...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
2
by: sxiao | last post by:
Hi, there I got a NullReferenceException when there are more than one users trying to open the same page at the same time. The senerio is: Two users logged into the web application using the...
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...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...

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.