• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 57
  • Last Modified:

System.NullReferenceException: "Object reference not set to an instance of an object." error

label1.Text = " You login as :" + ((Form)this.MdiParent).Controls["label1"].Text;
       
System.NullReferenceException: "Object reference not set to an instance of an object." error
0
Gökmen Anıl Yılmaz
Asked:
Gökmen Anıl Yılmaz
  • 4
  • 4
  • 4
  • +1
4 Solutions
 
AndyAinscowFreelance programmer / ConsultantCommented:
OK, that means one of the following:
this.MdiParent is null
((Form)this.MdiParent).Controls["label1"] is null
((Form)this.MdiParent).Controls["label1"].Text is null
Check which of those is the problem.  (I guess there is no control called label1 on the form)
0
 
Nitin SontakkeDeveloperCommented:
Is this also not sort of circular reference? What exactly are you trying achieve by picking up a value from a place and assigning it back to same place.

Hope I am looking at it correctly!

And by the way, where does Microsoft SQL Server comes into picture? Primary reason, I came here....
0
 
MishaProgrammerCommented:
((Form)this.MdiParent).Controls["label1"].Text   probable  is not null. This value is "".
 This exeption arise because ((Form)this.MdiParent).Controls["label1"] is null.
And null element have not property "Text"
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Nitin SontakkeDeveloperCommented:
Before child is shown, I think, Mdiparent must be explicitly assigned. In any case, that's what I just did. Without that got the NullReference Exception.

frmPanel mf = new frmPanel();
mf.MdiParent = this;
mf.Show();

Open in new window

0
 
Gökmen Anıl YılmazStudentAuthor Commented:
I want to print the name of the user who logged in
0
 
Nitin SontakkeDeveloperCommented:
Just before showing the form where you above code is, have you initialized it's MdiParent property? See my comment above, to get an idea.
0
 
Gökmen Anıl YılmazStudentAuthor Commented:
public partial class asd : Form
    {
        public asd()
        {
            InitializeComponent();
        }

        private void asd_Load(object sender, EventArgs e)
        {
            label1.Text = " You login as :" + ((Form)this.MdiParent).Controls["label1"].Text;
        }
    }

first use mdiparent
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
>>I want to print the name of the user who logged in

OK - see my first comment and determine which is giving a null value.  (Then it can be corrected)
0
 
Gökmen Anıl YılmazStudentAuthor Commented:
public partial class MDIParent1 : Form
    {
     

        public MDIParent1( string role)
        {
            InitializeComponent();
            label1.Text = role;
        }

        private void asdToolStripMenuItem_Click(object sender, EventArgs e)
        {
            asd ss = new asd();
            ss.Show();


        }
that's how it works here
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Oh dear.
Please look at this:
https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-create-mdi-child-forms

You're not creating a child form of the parent - hence a failure in your code
1
 
Gökmen Anıl YılmazStudentAuthor Commented:
Thank you Andy
0
 
Nitin SontakkeDeveloperCommented:
Precisely what is given in the hyperlink is what i had posted in my comment which you have accepted as an assisted solution. Anyway...
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
The link contains detailed instructions about creating and using MDI windows.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now