none
System.nuureferenceexception RRS feed

  • Question

  • After the execution of the program and
      when closing the main window I get the
    message System.NullReferenceException.That's the code:

    namespace Guifreaks.NavigationBar

    {

       partial class NaviControl

       {

          /// <summary>

          /// Required designer variable.

          /// </summary>

          private System.ComponentModel.IContainer components = null;
          /// <summary>

          /// Clean up any resources being used.

          /// </summary>

          /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

          protected override void Dispose(bool disposing)

          {

             if (disposing && (components != null))

             {

                components.Dispose();

             }

             base.Dispose(disposing);

          }
          #region Component Designer generated code
          /// <summary>

          /// Required method for Designer support - do not modify

          /// the contents of this method with the code editor.

          /// </summary>

          private void InitializeComponent()

          {

             components = new System.ComponentModel.Container();

          }

          #endregion

       }

    et marciet Thanks



    nour


    nour

    Wednesday, April 4, 2018 9:57 AM

Answers

  • You are not implementing the Dispose pattern correctly that's why you are getting this error. You need to check if the object is already disposed then don't call the method as it would cause exception which you have got above. The following is an example from MSDN docs how to implement the Dispose pattern in child class:

    using System;
    
    class DerivedClass : BaseClass
    {
       // Flag: Has Dispose already been called?
       bool disposed = false;
       
       // Protected implementation of Dispose pattern.
       protected override void Dispose(bool disposing)
       {
          if (disposed)
             return; 
          
          if (disposing) {
             // Free any other managed objects here.
             //
          }
          
          // Free any unmanaged objects here.
          //
          disposed = true;
          
          // Call the base class implementation.
          base.Dispose(disposing);
       }
    
       ~DerivedClass()
       {
          Dispose(false);
       }
    }

    You can read in detail on the following link:

    Implementing a Dispose method

    and you can also refer to the following post which might also be helpful:

    Implementing IDisposable correctly

    Hope it helps.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by Ehsan Sajjad Wednesday, April 4, 2018 10:21 AM
    • Marked as answer by noursila Wednesday, April 4, 2018 10:54 AM
    Wednesday, April 4, 2018 10:20 AM