PDA

View Full Version : System.NullReferenceException error


Pushapjit
03-31-2006, 03:13 AM
An exception 'System.NullReferenceException' has occured in the classifyPoly() code

// Loop round each of the vertices
for( int i = 0; i < Poly->nNumberOfVertices; ++i )

following is the full code for buildbsptree() and classifyPoly() function. classifyPoly() function is called from buildbsptree() function


//-----------------------------------------------------------------------------
// Name : buildBspTree (Recursive)
// Desc : Performs the actual BSP Compile when fed a polygon linked list and
// a valid node (first time this is called this will be the parent of
// the polygon linked list, and an already created root node.
//-----------------------------------------------------------------------------
void CMyD3DApplication::buildBspTree( NODE *CurrentNode,POLYGON *PolyList )
{
POLYGON *polyTest = NULL;
POLYGON *FrontList = NULL;
POLYGON *BackList = NULL;
POLYGON *NextPolygon = NULL;
POLYGON *FrontSplit = NULL;
POLYGON *BackSplit = NULL;

// Select the best splitter for this set of polygons
CurrentNode->Splitter = selectBestSplitter(PolyList);

polyTest = PolyList;

// Begin the loop until we reach the end of the linked list.
while( polyTest != NULL )
{
// Remember to store because polytest->Next could be altered
NextPolygon = polyTest->Next;

if( polyTest != CurrentNode->Splitter )
{
switch( classifyPoly( CurrentNode->Splitter, polyTest ) )
{
case POINTPOSITION_FRONT:
polyTest->Next = FrontList;
FrontList = polyTest;
break;

case POINTPOSITION_BACK:
polyTest->Next = BackList;
BackList = polyTest;
break;

case POINTPOSITION_SPANNING:
// Allocate two new polys for this fragment
FrontSplit = new POLYGON;
BackSplit = new POLYGON;
ZeroMemory(FrontSplit,sizeof(POLYGON));
ZeroMemory(BackSplit,sizeof(POLYGON));
// Split the poly into two fragments
splitPolygon( polyTest, CurrentNode->Splitter, FrontSplit, BackSplit);
// Delete the original poly
delete polyTest;
// Reshuffle linked list
FrontSplit->Next = FrontList;
FrontList = FrontSplit;
BackSplit->Next = BackList;
BackList = BackSplit;
break;

default:
break;
}
}

// Move onto the next polygon
polyTest = NextPolygon;
}

// If there is nothing left in the front list then add an empty node here,
// otherwise carry on building the tree
if( FrontList == NULL )
{
NODE *leafnode = new NODE;
ZeroMemory(leafnode,sizeof(leafnode));
leafnode->bIsLeaf = true;
leafnode->bIsSolid = false;
leafnode->Front = NULL;
leafnode->Back = NULL;
leafnode->Splitter = NULL;
CurrentNode->Front = leafnode;
}
else
{
NODE *newnode = new NODE;
ZeroMemory(newnode,sizeof(newnode));
newnode->bIsLeaf = false;
CurrentNode->Front = newnode;
buildBspTree(newnode,FrontList);
} // End if frontlist is empty

// If there is nothing left in the back list then add a solid node here,
// otherwise carry on building the tree
if( BackList == NULL )
{
NODE *leafnode = new NODE;
ZeroMemory(leafnode,sizeof(leafnode));
leafnode->bIsLeaf = true;
leafnode->bIsSolid = true;
leafnode->Front = NULL;
leafnode->Back = NULL;
leafnode->Splitter = NULL;
CurrentNode->Back = leafnode;;
}
else
{
NODE *newnode = new NODE;
ZeroMemory(newnode,sizeof(newnode));
newnode->bIsLeaf = false;
CurrentNode->Back = newnode;
buildBspTree(newnode,BackList);
}
}




//-----------------------------------------------------------------------------
// Name : classifyPoly()
// Desc : Classifies a polygon against the plane passed
//-----------------------------------------------------------------------------
int CMyD3DApplication::classifyPoly( POLYGON *Plane, POLYGON *Poly )
{
D3DXVECTOR3 *vec1 = (D3DXVECTOR3*)&Plane->VertexList[0];
int Infront = 0;
int Behind = 0;
int OnPlane = 0;
float result;

// Loop round each of the vertices
for( int i = 0; i < Poly->nNumberOfVertices; ++i )
{
D3DXVECTOR3 *vec2 = (D3DXVECTOR3 *)&Poly->VertexList[i];
D3DXVECTOR3 Direction = (*vec1) - (*vec2);

result = D3DXVec3Dot(&Direction,&Plane->Normal);
// Tally up the position of each vertex
if( result > 0.001f )
++Behind;
else if( result < -0.001f )
++Infront;
else
{
++OnPlane;
++Infront;
++Behind;
}
}

if( OnPlane == Poly->nNumberOfVertices )
return POINTPOSITION_FRONT;

if( Behind == Poly->nNumberOfVertices )
return POINTPOSITION_BACK;

if( Infront == Poly->nNumberOfVertices )
return POINTPOSITION_FRONT;

return POINTPOSITION_SPANNING;
}

pater
03-31-2006, 04:38 AM
Did you look with the debugger at your code? Which variable is NULL at the time of crash?