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
Gökmen Anıl YılmazStudentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.