Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 886
  • Last Modified:

Object reference not set to an instance of an object

I'm designing a project with 4 forms.  All of the forms interact with each other, and up until now, I've had no problem at all with this.  Now when I try to execute one of my own reports, it throws the above message, here is the exact exception: "An error occurred creating the form. See Exception.InnerException for details.  The error is: Object reference not set to an instance of an object."

The code will follow.  I have used and reused this report without error or issue up until about an hour ago, when this started happening.  Form4 is unchanged since then.  Form3 has only had a couple of new labels added, and I don't see how adding new labels would throw this exception.
Dim i As Integer = 0
                    Dim subtotalcerts As Integer = 0
                    Dim totalcerts As Integer = 0
                    Dim subtotal As Double = 0
                    Dim total As Double = 0
 
                    Form1.Enabled = True
 
                    If (Form3.chkClass1.Checked = True) Or (Form3.chkDesc1.Checked = True) Then
                        _2008_WorkingTableAdapter.TotalsDesc1(_2008_Working_NewDataSet._2008_Working, Form3.Desc1)
                        subtotalcerts = 0
                        subtotal = 0
                        For i = 0 To Me.DataGridView1.RowCount - 1
                            subtotalcerts += Me.DataGridView1.Rows(i).Cells("OfSharesDataGridViewTextBoxColumn").Value
                        Next
                        For i = 0 To Me.DataGridView1.RowCount - 1
                            subtotal += Me.DataGridView1.Rows(i).Cells("TotalValueDataGridViewTextBoxColumn").Value
                        Next
                        Form4.TextBox1.Text = subtotalcerts
                        Form4.TextBox28.Text = Format(subtotal, "###,###,###.00")
                        totalcerts += subtotalcerts
                        total += subtotal
                    End If
 
                    If (Form3.chkClass2.Checked = True) Or (Form3.chkDesc2.Checked = True) Then
                        _2008_WorkingTableAdapter.TotalsDesc1(_2008_Working_NewDataSet._2008_Working, Form3.Desc2)
                        subtotalcerts = 0
                        subtotal = 0
                        For i = 0 To Me.DataGridView1.RowCount - 1
                            subtotalcerts += Me.DataGridView1.Rows(i).Cells("OfSharesDataGridViewTextBoxColumn").Value
                        Next
                        For i = 0 To Me.DataGridView1.RowCount - 1
                            subtotal += Me.DataGridView1.Rows(i).Cells("TotalValueDataGridViewTextBoxColumn").Value
                        Next
                        Form4.TextBox2.Text = subtotalcerts
                        Form4.TextBox27.Text = Format(subtotal, "###,###,###.00")
                        totalcerts += subtotalcerts
                        total += subtotal
                    End If
 
                    If (Form3.chkClass3.Checked = True) Or (Form3.chkDesc3.Checked = True) Then
                        _2008_WorkingTableAdapter.TotalsDesc1(_2008_Working_NewDataSet._2008_Working, Form3.Desc3)
                        subtotalcerts = 0
                        subtotal = 0
                        For i = 0 To Me.DataGridView1.RowCount - 1
                            subtotalcerts += Me.DataGridView1.Rows(i).Cells("OfSharesDataGridViewTextBoxColumn").Value
                        Next
                        For i = 0 To Me.DataGridView1.RowCount - 1
                            subtotal += Me.DataGridView1.Rows(i).Cells("TotalValueDataGridViewTextBoxColumn").Value
                        Next
                        Form4.TextBox3.Text = subtotalcerts
                        Form4.TextBox26.Text = Format(subtotal, "###,###,###.00")
                        totalcerts += subtotalcerts
                        total += subtotal
                    End If

Open in new window

0
Sereou
Asked:
Sereou
1 Solution
 
SereouAuthor Commented:
Just as a footnote, the exception thrown is at line 19 of this snippet.
0
 
Daniel WilsonCommented:
What is the context of this code block?

Is Form4 visible from it?

Does Form4 contain a TextBox1?
0
 
SereouAuthor Commented:
Form4 does contain TextBox1, and I'm really not sure what you mean by 'is it visible from it'

This code is in the EnabledChanged event of Form2 (and under an if statement making sure enabled = true), I need it to happen every time Form2 becomes enabled.  

Form4 is a public class.

What it looks like now is that every reference I have to Form4 in all of my code throws this exception.  All of this code worked before very well.  Is there something I could have accidentally changed from then til now to make form4 not accessible by the others?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TechTiger007Commented:
Check if Form4 or Form4.TextBox1 is null.
0
 
SereouAuthor Commented:
I'm really not sure how to check if a Form is null.  It's not the text box I'm having problems with, it's accessing the form from any other form.  I can't do anything at all with Form4 from the other forms, no reference that I have works.
0
 
MTEXXCommented:
I'm surprised you don't get an invalid cast error...
Form4.TextBox1.Text = subtotalcerts.ToString

Open in new window

0
 
SereouAuthor Commented:
VB 2008 converts from integer to string freely without needing the additional code

It's not the text box that there is a problem with though, it's referring to ANY part of the form at all.  Even if I want to, for instance, do Form4.Show() or Form4.Hide()  I get the same exception listed above.
0
 
SereouAuthor Commented:
I'm really stumped here guys.  All 3 of the other forms interact freely.  But anytime I try to make any reference to Form4 from any of the other 3, I get that exception.  And I didn't before, I was using Form4 freely before too.

What could I have possibly done to make form4 references throw that error?
0
 
Daniel WilsonCommented:
What you're doing should work.  I just verified ... created a little app w/ 2 forms.  In Form1's Form_Load I called From2.Show().  Works fine.

I even looked through the various files in a text editor to see where the declarations are going on ... and I don't see anything that could easily have been corrupted.

Would you try removing Form4 from your project, then adding it back?  I'm suspecting it's some setting that VS messed up.
0
 
SereouAuthor Commented:
I'll try that.  I'm going to backup my project, and see if I can manage to make it work that way.

I've thought about copying all the code, and trying to rebuild it, but my deadline is tomorrow, I don't really have time for that unless I absolutely need to, lol.
0
 
Daniel WilsonCommented:
>>I've thought about copying all the code, and trying to rebuild it

Shouldn't need to do that.

But removing the form from the project, then doing an Add Existing Item ... might work.
0
 
SereouAuthor Commented:
No such luck, what am I doing wrong here?  No matter what I do, it seems like none of the other forms want to have any reference to form4.  I even, in deperation, tried making a new form (form5, lol) and put all of the same controls onto it, and threw my code into it, and boom, same exception.  It's something in my code =(
0
 
Daniel WilsonCommented:
OK, but if you do a Form6 that doesn't  have all that code from Form4 -- just an empty form or maybe a textbox on it -- that works?

If so ... would you post the code from Form4?
0
 
SereouAuthor Commented:
Well, I did one better.  I removed all of the code from form4, and just did a button from another form (that previously was not working) that is just Form4.Show().  Once I deleted the code, it worked.

So I went on to start deleting snippets, running it, replacing, and deleting another til I found out what it is.  I'll post the entire code from the form, but I warn you, it's very ugly.  I did ALOT of very lazy scripting on form4 (This form is seldom seen).   I'll explain in my next comment after the code what worked, and maybe you can help me figure out why...
Public Class Form4
 
    Dim color As Color = Me.BackColor()
    Dim color1 As Color = Label1.BackColor()
    Dim color2 As Color = Label2.BackColor()
    Dim color3 As Color = Label3.BackColor()
    Dim color4 As Color = Label4.BackColor()
    Dim color5 As Color = Label5.BackColor()
    Dim color6 As Color = Label6.BackColor()
    Dim color7 As Color = Label7.BackColor()
    Dim color8 As Color = Label8.BackColor()
    Dim color9 As Color = Label9.BackColor()
    Dim color10 As Color = Label10.BackColor()
    Dim color11 As Color = Label11.BackColor()
    Dim color12 As Color = Label12.BackColor()
    Dim color13 As Color = Label13.BackColor()
    Dim color14 As Color = Label14.BackColor()
    Dim color15 As Color = Label15.BackColor()
    Dim color16 As Color = Label16.BackColor()
    Dim color17 As Color = Label17.BackColor()
 
    Private Sub Form4_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closing
        Me.Enabled = False
        Form2.DataGridView1.Enabled = False
    End Sub
 
    Private Sub Form4_Enabled(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.EnabledChanged
        If Me.Enabled = True Then
            If (Form3.chkClass1.Checked = True) Or (Form3.chkDesc1.Checked = True) Then
                Label1.Visible = True
                TextBox1.Visible = True
                TextBox28.Visible = True
            Else
                Label2.Location = Label1.Location
                TextBox2.Location = TextBox1.Location
                TextBox27.Location = TextBox28.Location
            End If
 
            If (Form3.chkClass2.Checked = True) Or (Form3.chkDesc2.Checked = True) Then
                Label2.Visible = True
                TextBox2.Visible = True
                TextBox27.Visible = True
                Label3.Top = Label2.Top + 35
                TextBox3.Top = TextBox2.Top + 35
                TextBox26.Top = TextBox27.Top + 35
            Else
                Label3.Top = Label2.Top
                TextBox3.Top = TextBox2.Top
                TextBox26.Top = TextBox27.Top
            End If
 
            If (Form3.chkClass3.Checked = True) Or (Form3.chkDesc3.Checked = True) Then
                Label3.Visible = True
                TextBox3.Visible = True
                TextBox26.Visible = True
                Label4.Top = Label3.Top + 35
                TextBox4.Top = TextBox3.Top + 35
                TextBox25.Top = TextBox26.Top + 35
            Else
                Label4.Top = Label3.Top
                TextBox4.Top = TextBox3.Top
                TextBox25.Top = TextBox26.Top
            End If
 
            If (Form3.chkClass4.Checked = True) Or (Form3.chkDesc4.Checked = True) Then
                Label4.Visible = True
                TextBox4.Visible = True
                TextBox25.Visible = True
                Label5.Top = Label4.Top + 35
                TextBox5.Top = TextBox4.Top + 35
                TextBox24.Top = TextBox25.Top + 35
            Else
                Label5.Top = Label4.Top
                TextBox5.Top = TextBox4.Top
                TextBox24.Top = TextBox25.Top
            End If
 
            If (Form3.chkClass5.Checked = True) Or (Form3.chkDesc5.Checked = True) Then
                Label5.Visible = True
                TextBox5.Visible = True
                TextBox24.Visible = True
                Label6.Top = Label5.Top + 35
                TextBox6.Top = TextBox5.Top + 35
                TextBox23.Top = TextBox24.Top + 35
            Else
                Label6.Top = Label5.Top
                TextBox6.Top = TextBox5.Top
                TextBox23.Top = TextBox24.Top
            End If
 
            If (Form3.chkClass6.Checked = True) Or (Form3.chkDesc6.Checked = True) Then
                Label6.Visible = True
                TextBox6.Visible = True
                TextBox23.Visible = True
                Label7.Top = Label6.Top + 35
                TextBox7.Top = TextBox6.Top + 35
                TextBox22.Top = TextBox23.Top + 35
            Else
                Label7.Top = Label6.Top
                TextBox7.Top = TextBox6.Top
                TextBox22.Top = TextBox23.Top
            End If
 
            If (Form3.chkClass7.Checked = True) Or (Form3.chkDesc7.Checked = True) Then
                Label7.Visible = True
                TextBox7.Visible = True
                TextBox22.Visible = True
                Label8.Top = Label7.Top + 35
                TextBox8.Top = TextBox7.Top + 35
                TextBox21.Top = TextBox22.Top + 35
            Else
                Label8.Top = Label7.Top
                TextBox8.Top = TextBox7.Top
                TextBox21.Top = TextBox22.Top
            End If
 
            If (Form3.chkClass8.Checked = True) Or (Form3.chkDesc8.Checked = True) Then
                Label8.Visible = True
                TextBox8.Visible = True
                TextBox21.Visible = True
                Label9.Top = Label8.Top + 35
                TextBox9.Top = TextBox8.Top + 35
                TextBox20.Top = TextBox21.Top + 35
            Else
                Label9.Top = Label8.Top
                TextBox9.Top = TextBox8.Top
                TextBox20.Top = TextBox21.Top
            End If
 
            If (Form3.chkClass9.Checked = True) Or (Form3.chkDesc9.Checked = True) Then
                Label9.Visible = True
                TextBox9.Visible = True
                TextBox20.Visible = True
                Label10.Top = Label9.Top + 35
                TextBox10.Top = TextBox9.Top + 35
                TextBox19.Top = TextBox20.Top + 35
            Else
                Label10.Top = Label9.Top
                TextBox10.Top = TextBox9.Top
                TextBox19.Top = TextBox20.Top
            End If
 
            If (Form3.chkClass10.Checked = True) Or (Form3.chkDesc10.Checked = True) Then
                Label10.Visible = True
                TextBox10.Visible = True
                TextBox19.Visible = True
                Label11.Top = Label10.Top + 35
                TextBox11.Top = TextBox10.Top + 35
                TextBox18.Top = TextBox19.Top + 35
            Else
                Label11.Top = Label10.Top
                TextBox11.Top = TextBox10.Top
                TextBox18.Top = TextBox19.Top
            End If
 
            If (Form3.chkClass11.Checked = True) Or (Form3.chkDesc11.Checked = True) Then
                Label11.Visible = True
                TextBox11.Visible = True
                TextBox18.Visible = True
                Label12.Top = Label11.Top + 35
                TextBox12.Top = TextBox11.Top + 35
                TextBox17.Top = TextBox18.Top + 35
            Else
                Label12.Top = Label11.Top
                TextBox12.Top = TextBox11.Top
                TextBox17.Top = TextBox18.Top
            End If
 
            If (Form3.chkClass12.Checked = True) Or (Form3.chkDesc12.Checked = True) Then
                Label12.Visible = True
                TextBox12.Visible = True
                TextBox17.Visible = True
                Label13.Top = Label12.Top + 35
                TextBox13.Top = TextBox12.Top + 35
                TextBox16.Top = TextBox17.Top + 35
            Else
                Label13.Top = Label12.Top
                TextBox13.Top = TextBox12.Top
                TextBox16.Top = TextBox17.Top
            End If
 
            If (Form3.chkClass13.Checked = True) Or (Form3.chkDesc13.Checked = True) Then
                Label13.Visible = True
                TextBox13.Visible = True
                TextBox16.Visible = True
                Label14.Top = Label13.Top + 35
                TextBox14.Top = TextBox13.Top + 35
                TextBox15.Top = TextBox16.Top + 35
            Else
                Label14.Top = Label13.Top
                TextBox14.Top = TextBox13.Top
                TextBox15.Top = TextBox16.Top
            End If
        End If
    End Sub
 
    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click
        PrintForm1.PrinterSettings.DefaultPageSettings.Margins.Left = 175
        PrintForm1.PrinterSettings.DefaultPageSettings.Margins.Right = 175
        Me.BackColor = color.White
        Label1.BackColor = color.White
        Label2.BackColor = color.White
        Label3.BackColor = color.White
        Label4.BackColor = color.White
        Label5.BackColor = color.White
        Label6.BackColor = color.White
        Label7.BackColor = color.White
        Label8.BackColor = color.White
        Label9.BackColor = color.White
        Label10.BackColor = color.White
        Label11.BackColor = color.White
        Label12.BackColor = color.White
        Label13.BackColor = color.White
        Label14.BackColor = color.White
        Label15.BackColor = color.White
        Label16.BackColor = color.White
        Label17.BackColor = color.White
        cmdPrint.Hide()
        lblCountrymarkheader.Show()
        lblDate.Show()
        lblDate.Text = Today
    End Sub
 
    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.SelectionStart = 0
        TextBox1.SelectionLength = 0
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        PrintForm1.Print()
        lblCountrymarkheader.Hide()
        lblDate.Hide()
        cmdPrint.Show()
        Me.BackColor = color
        Label1.BackColor = color1
        Label2.BackColor = color2
        Label3.BackColor = color3
        Label4.BackColor = color4
        Label5.BackColor = color5
        Label6.BackColor = color6
        Label7.BackColor = color7
        Label8.BackColor = color8
        Label9.BackColor = color9
        Label10.BackColor = color10
        Label11.BackColor = color11
        Label12.BackColor = color12
        Label13.BackColor = color13
        Label14.BackColor = color14
        Label15.BackColor = color15
        Label16.BackColor = color16
        Label17.BackColor = color17
        Timer1.Enabled = False
    End Sub
End Class

Open in new window

0
 
SereouAuthor Commented:
Upon removing lines 3-20, as well as 235-251, the form began to work again!  What is in those lines that made it wrong?
0
 
Daniel WilsonCommented:
When you do a Form4.Anything the first time, it's instantiating an object of type Form4 and assigning it to variable Form4.  It's as if there's a line of code:
Dim Form4 as New Form4

so ... in order to initialize the form it's initializing the colors, based on the values in the labels that are on the form that may not be initialized.

I would, in lines 3-20, just have the Dim Color1 as Color portion and assign the values (Color1 = label1.BackColor) in Form_Load.

I think that will get ti working again -- w/o changing lines 235-251.
0
 
SereouAuthor Commented:
Yes, that worked!  Thank you for helping me understand it too, great explanation man.  Thanks so much for all of your help =)
0
 
SereouAuthor Commented:
Sorry to have made this so complicated, I should have just posted the entire form's code from the start.  Thanks a ton.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Tackle projects and never again get stuck behind a technical roadblock.
Join Now