CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Managed C++ and C++/CLI
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Managed C++ and C++/CLI Discuss Managed C++ and .NET-specific questions related to C++.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old February 25th, 2002, 09:52 AM
    wakthar wakthar is offline
    Junior Member
     
    Join Date: Nov 2001
    Posts: 12
    wakthar is an unknown quantity at this point (<10)
    System.NullReferenceException error in VC++.NET

    Hi,

    I have a problem which I dont know whether its to do with proper construction of an object or with releasing memory in a destructor.

    What I want to be able to do is have for example an array of strings so I can do the following

    pszChannel[0] = "Hello";
    pszChannel[1] = "World";
    pszChannel[2] = "!!!!!";

    so I can do for example

    strcpy(m_pObject->pszChannel[0], "HELLO");

    in another class.


    I have created an object which has the following properties

    #define MAX_NUM_CHANS = 3
    #define MAX_STRING_LEN = 32

    char* pszChannel[MAX_NUM_CHANS+1]; // IS IT OK
    char* pszPart[MAX_NUM_CHANS+1]; // TO DO THIS
    char* pszRange[MAX_NUM_CHANS+1]; // CONSTRUCTION
    char szName[MAX_STRING_LEN+1]; // ???????
    SAFEARRAY *psaDataArray;

    What I want is to have the ability for the object to contain the following data
    for example in the default contructor

    CTH::CTH()
    {
    for (int i=0; ipszChannel[i] = new char[96];
    strcpy(this->pszChannel[i], "");
    this->pszPart[i] = new char[96];
    strcpy(this-> pszPart [i], "");
    this->pszRange[i] = new char[96];
    strcpy(this-> pszRange [i], "");
    }
    this->szName = “Rotate”;
    }



    I can create the object fine and populate the data properties OK but when I want to delete the object
    how do I delete the properties in the destructor ? What I ultimately require is to delete the object and free up all memory associated with this object.

    If I try the following in the destructor I get an error

    CTH::~CTH()
    {
    for (int i=0; ipszChannel[i];
    delete [] this->pszPart[i];
    delete [] this->pszRange[i];
    }
    this->szName = “”;

    ::SafeArrayDestroy(psaDataArray);
    }



    ========== Exception Text ==========
    System.NullReferenceException: Value null was found where an instance of an object was required.
    at THLib.THIT.FileClose()
    at THTest.Form1.Button1_Click(Object sender, EventArgs e) in C:\1\source\THTest\Form1.vb:line 92
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)












    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Managed C++ and C++/CLI


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 05:20 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009