locked
object reference not set to an instance of an object RRS feed

  • Question

  • Hi,

    I am using the user control which is using singleton design pattern. In the designer form, I have added the user control. If I reopen the designer file, getting " object reference not set to an instance of an object" error. Not able to view the form designer file.

    Please send the quick response.

    Thanks,

    Mala

    Wednesday, June 8, 2016 8:44 AM

Answers

  • Hi Malasatheesh,

    >>"I tried adding the user control in new form."

    I realized that the reason of the exception is there is no public construction method in your user control. So the designer doesn't know how to show your user control. You could add the definition of userControl11 to code cs file instead of drag this control to designer.

    public Form1()
    {
        InitializeComponent();
    
        this.userControl11 = WindowsFormsControlLibrary1.UserControl1.Instance;
        this.Controls.Add(userControl11);
    }
    
    private WindowsFormsControlLibrary1.UserControl1 userControl11;
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by DotNet Wang Thursday, June 16, 2016 9:13 AM
    • Marked as answer by DotNet Wang Monday, June 20, 2016 8:58 AM
    Thursday, June 16, 2016 7:11 AM

All replies

  • A visual user control using the singleton design pattern??

    Not sure I understand how that would work.

    Can you post a a code sample of your user control?

    Wednesday, June 8, 2016 9:20 AM
  • Probably something is wrong in your User Control. Show some details about your singleton.

    If you want to remove it from the form and be able to work with form before you solve the problems, then try opening the code files, such as Form1.designer.cs and Form1.cs, and remove all of the lines that are related to bad control. (Also backup your files).



    • Edited by Viorel_MVP Wednesday, June 8, 2016 9:28 AM
    Wednesday, June 8, 2016 9:27 AM
  • It means that a process is running with a not instanced variable. You have to look in your designer file what it can be.

    Success
    Cor

    Wednesday, June 8, 2016 9:52 AM
  • Thanks a lot for your quick replies.

    Here is the code snippet of singleton which is used in user control.

    private usercontrol1 ()
        {  InitializeComponent();
        }

     private static volatile usercontrol1 instance;
     private static object syncRoot = new Object();  
     public static usercontrol1 Instance
        {
      get {
       if (instance == null)
       {
        lock (syncRoot)
        {
         if (instance == null)
          instance = new usercontrol1 ();    
        }
       }
        return instance; 
      }

    And in the designer file, below line is added to initialize.

    /* Initiation */
       this.usercontrol1 = usercontrol.usercontrol1 .Instance;
      
       /* Properties */
       this.usercontrol1.Location = new System.Drawing.Point(4, 58);
       this.usercontrol1.Margin = new System.Windows.Forms.Padding(5);
       this.usercontrol1.Name = "usercontrol";
       this.usercontrol1.Size = new System.Drawing.Size(594, 312);
       this.usercontrol1.TabIndex = 115;
       this.usercontrol1.Visible = false;
      
       private usercontrol.usercontrol1 usercontrol1;
     
     /* Adding control to form */
     this.Controls.Add(this.usercontrol1);


    In run time, able to view the control but not able to view the designer file. Please guide me.

    Regards,
    Mala

    Friday, June 10, 2016 9:17 AM
  • Hi Malasatheesh,

    Thank you for your post. Is your project type Windows Forms. If yes, I am afraid you can't edit designer file, because the designer file is generated by your design form. It will be covered after running your application. I suggest you copy following code to .cs code file after InitializeComponent method.  

    InitializeComponent();

    /* Initiation */ this.usercontrol1 = usercontrol.usercontrol1 .Instance; /* Properties */ this.usercontrol1.Location = new System.Drawing.Point(4, 58); this.usercontrol1.Margin = new System.Windows.Forms.Padding(5); this.usercontrol1.Name = "usercontrol"; this.usercontrol1.Size = new System.Drawing.Size(594, 312); this.usercontrol1.TabIndex = 115; this.usercontrol1.Visible = false; this.Controls.Add(this.usercontrol1);

    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by DotNet Wang Tuesday, June 14, 2016 9:05 AM
    Tuesday, June 14, 2016 9:05 AM
  • Hi Li Wang,

    I had copied the above mentioned code inside "Form1.designer.cs" file. Still facing problem in opening the designer file in IDE.

    Please suggest.

    Regards,

    Mala

    Wednesday, June 15, 2016 4:45 AM
  • Hi Malasatheesh,

    Why not re-create a new form, and please remember don't edit .design.cs file.  I think similar issues will not happen again.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 15, 2016 5:23 AM
  • Hi Li Wang,

    I tried adding the user control in new form. Added below line in InitializeComponent function. Still while opening the designer file, its giving error saying "the variable usercontrol1 is either undeclared or never assigned'. But I can able to run the form after adding the below line Initializecomponent function.

    this.userControl11 = WindowsFormsControlLibrary1.UserControl1.Instance;

    Please reply as early as possible.

    Regards,

    Mala

    Thursday, June 16, 2016 4:48 AM
  • Hi Malasatheesh,

    >>"I tried adding the user control in new form."

    I realized that the reason of the exception is there is no public construction method in your user control. So the designer doesn't know how to show your user control. You could add the definition of userControl11 to code cs file instead of drag this control to designer.

    public Form1()
    {
        InitializeComponent();
    
        this.userControl11 = WindowsFormsControlLibrary1.UserControl1.Instance;
        this.Controls.Add(userControl11);
    }
    
    private WindowsFormsControlLibrary1.UserControl1 userControl11;
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by DotNet Wang Thursday, June 16, 2016 9:13 AM
    • Marked as answer by DotNet Wang Monday, June 20, 2016 8:58 AM
    Thursday, June 16, 2016 7:11 AM
  • Thanks for your fast reply Li Wang.

    Yes I agree with you. User control class doesn't have public constructor. After adding the below code, able to view Form1.cs[design] file.

          private WindowsFormsControlLibrary1.UserControl1 userControl11;
            public Form1()
            {
                InitializeComponent();
               this.userControl11 = WindowsFormsControlLibrary1.UserControl1.Instance;
              
                this.userControl11.BackColor = System.Drawing.SystemColors.ActiveCaption;
                this.userControl11.Location = new System.Drawing.Point(42, 21);
                this.userControl11.Name = "userControl11";
                this.userControl11.Size = new System.Drawing.Size(231, 22);
                this.userControl11.TabIndex = 3;

                this.Controls.Add(userControl11);
            }

    Now not facing any issue in running the application as well as in viewing the design file. Thing is now usercontrol is not visible in design file as the entire usercontrol code is added in Form1.cs file. Is there any way to view the usercontrol in design file.

    Regards,

    Mala

    Thursday, June 16, 2016 9:02 AM
  • Hi Malasatheesh,

    >>"Is there any way to view the usercontrol in design file."

    I am afraid we can't do it. Because it is not recommanded to change the design file.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 16, 2016 9:12 AM