Jump to content
  • Advertisement
Sign in to follow this  
Pushapjit

System.NullReferenceException error

This topic is 4441 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
        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;
}

Share this post


Link to post
Share on other sites
Advertisement
Use the debugger to examine the value of Poly. It seems like it's NULL. Then use the call stack to trace back and find out what it's NULL.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!