0

When running my VB.Net code (below), I get the following error:

An unhandled exception of type 'System.InvalidOperationException' occurred in CropModel.exe An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.

Private Class Frm_Main     

      Private Sub Form_MainControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Reinsurance_YearTableAdapter.Fill(Me.CUNACropModelDataSet.Reinsurance_Year)
        Me.Ref_CropTableAdapter.Fill(Me.CUNACropModelDataSet.ref_Crop)
        Me.MdiParent = MDIParent1

      End Sub

      Private Sub Button_ModelLoss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_ModelLoss.Click

            Frm_LossModel_Select.MdiParent = MDIParent1
            Frm_LossModel_Select.Show()
            Frm_LossModel_Select.Activate()
            'Me.Close()

       End Sub
 End Class

The debug points the issue in the following line:

Frm_LossModel_Select.MdiParent = MDIParent1

Seems this error is very general. Is it possible to know where the actual error is being caused in Frm_LossModel_Select (if it is even being caused there)? The only things changed in that form were DataSet names and table names from SQL and I made sure those were all being referenced properly.

If additional code is needed for this issue, I'd be more than willing to provide.

Thanks!

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Kristina
  • 1
  • 2
  • I doubt that is the correct line - button click event do not normally happen while creating the form. You could compile for AnyCPU and run it in the IDE and likely get a better idea where it is – Ňɏssa Pøngjǣrdenlarp Mar 05 '15 at 21:37
  • @Plutonix That's the line the debug pointed me to with Any CPU setting – Kristina Mar 05 '15 at 23:00
  • Well the error says something bad happened while *creating the form*; the line you indicate exists in a button click event. So the question is how did that event get executed? When you run it, look in the Output window for any First Chance Exceptions for more clues. Maybe include any initialization code too. – Ňɏssa Pøngjǣrdenlarp Mar 05 '15 at 23:09
  • `Frm_LossModel_Select` is another form and the statement `Frm_LossModel_Select.MdiParent = MDIParent1` causes VB.NET (for reasons of [backwards compatibility](http://stackoverflow.com/a/4699360/2278086)) to try and create a default instance of it, so it is possible (likely?) that your NRE is coming from the constructor for `Frm_LossModel_Select`, and that is what the "An error occurred creating the form" part means. – Mark Mar 05 '15 at 23:20
  • @Plutonix There's a bunch of code before this that creates buttons and when they're clicked, a lot of calculations go on. The window with the button comes up fine but when I click on it, the debugger takes me to that one line. I'll check the output to see if there's any other explanations but last I remember there was not much there. Thanks. – Kristina Mar 05 '15 at 23:23
  • I'd like to see that "bunch of code". There is a limit to what you can do with forms and controls in the initialization code, ctor and Form Load. [Covered here](http://stackoverflow.com/a/26761773/1070452) – Ňɏssa Pøngjǣrdenlarp Mar 05 '15 at 23:26
  • Have you followed the explicit instructions provided in the error message? See Exception.InnerException for details. – jmcilhinney Mar 06 '15 at 00:28

0 Answers0