0

I have narrowed down what is causing the issue, but uncertain how to remedy. The issue is that once the InputCtrl ic = new InputCtrl(); is called from within my method PopulateData() I receive an error of

Object reference is not set to an instance of an object

Now to me, I am setting it to an instance of an object with the variable ic

Backstory on what is going on here, I have a form loading, that on load, should generate a "new screen", essentially a "blank slate" for the user to begin inputting data into. Now the top 3 text boxes will populate no problem, the only reason I can think of is that they exist on the main form, while the 4 below exist on the input control.

What should be altered in this syntax so that it executes accordingly?

    private void MainForm_Load(object sender, EventArgs e)
    {
        NewScreen("new", new DFC());
    }


    void NewScreen(string strNewTabName, DFC validinput)
    {
        InputCtrl nsc = new InputCtrl();
        nsc.Location = new Point(0, 0);
        nsc.Anchor = (AnchorStyles)(AnchorStyles.Top | AnchorStyles.Left);
        nsc.Size = new Size(828, 689);
        nsc.Name = "DrawingStageCtrl1";
        nsc.TabIndex = 0;
        nsc.Dock = DockStyle.Fill;
        nsc.Setvalidinput(validinput);

        TabPage NewtabPage = new TabPage();
        NewtabPage.Location = new Point(4, 22);
        NewtabPage.Name = "tabPage1";
        NewtabPage.Padding = new Padding(3);
        NewtabPage.Size = new Size(828, 689);
        NewtabPage.TabIndex = 0;
        NewtabPage.Text = strNewTabName;
        NewtabPage.UseVisualStyleBackColor = true;
        NewtabPage.Controls.Add(nsc);
        MainTabCtrl.Controls.Add(NewtabPage);

        MainTabCtrl.SelectedTab = NewtabPage;
        CurrentSelectedCtrl = nsc;
        PopulateData(validinput);
    }

    public void PopulateData(DFC validinput)
    {

        txta.Text = validinput.a.ToString();
        txtb.Text = validinput.m_b.ToString();
        txtc.Text = validinput.m_c.ToString();

        InputCtrl ic = new InputCtrl();
        ic.txtpb.Text = validinput.m_pb.ToString();
        ic.d.Text = validinput.m_d.ToString();
        ic.pn.Text = validinput.m_pn.ToString();
        ic.ls.Text = validinput.m_ls.ToString();
    }
Smith Stanley
  • 461
  • 1
  • 8
  • 25
  • @Craig W. - I tried to use the suggestions posted in the dupe, but am still getting the error, for example I added `if (validinput.m_pb.ToString() != null) { ic.txtpb.Text = validinput.m_pb.ToString(); }` but this still gives me the object not set error – Smith Stanley Aug 15 '17 at 17:39
  • Most likely a or m_b or m_c or m_pb or m_d or m_pn are null. – LarsTech Aug 15 '17 at 17:41
  • @LarsTech - yes it is null. I try to account for it with my != null check, but the error is thrown on the check line ? – Smith Stanley Aug 15 '17 at 17:41
  • You can't do a ToString on a null object. Try `validinput.m_pb != null` – LarsTech Aug 15 '17 at 17:42
  • @LarsTech - that got it. Thank you!!! I learned you are unable to use `.ToString()` on a null object! – Smith Stanley Aug 15 '17 at 17:44

0 Answers0