Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Object reference not set to an instance of an object

Posted on 2008-10-09
18
Medium Priority
?
885 Views
Last Modified: 2013-11-27
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
Comment
Question by:Sereou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
18 Comments
 

Author Comment

by:Sereou
ID: 22678731
Just as a footnote, the exception thrown is at line 19 of this snippet.
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22678804
What is the context of this code block?

Is Form4 visible from it?

Does Form4 contain a TextBox1?
0
 

Author Comment

by:Sereou
ID: 22678854
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 13

Expert Comment

by:TechTiger007
ID: 22678997
Check if Form4 or Form4.TextBox1 is null.
0
 

Author Comment

by:Sereou
ID: 22679197
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
 
LVL 2

Expert Comment

by:MTEXX
ID: 22679198
I'm surprised you don't get an invalid cast error...
Form4.TextBox1.Text = subtotalcerts.ToString

Open in new window

0
 

Author Comment

by:Sereou
ID: 22679271
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
 

Author Comment

by:Sereou
ID: 22679361
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
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22679423
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
 

Author Comment

by:Sereou
ID: 22679442
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
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22679526
>>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
 

Author Comment

by:Sereou
ID: 22679683
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
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22679699
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
 

Author Comment

by:Sereou
ID: 22679826
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
 

Author Comment

by:Sereou
ID: 22679840
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
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 2000 total points
ID: 22680025
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
 

Author Comment

by:Sereou
ID: 22680122
Yes, that worked!  Thank you for helping me understand it too, great explanation man.  Thanks so much for all of your help =)
0
 

Author Closing Comment

by:Sereou
ID: 31504660
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question