4

I can't figure out what the issue is here. I started project from scratch, went to debug, and received error:

System.InvalidOperationException was unhandled Message=An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.

I am not understand why this error is occurring in an auto-generated file. Here is the code in it's entirety:

    '------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:4.0.30319.269
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict On
Option Explicit On


Namespace My

    'NOTE: This file is auto-generated; do not modify it directly.  To make changes,
    ' or if you encounter build errors in this file, go to the Project Designer
    ' (go to Project Properties or double-click the My Project node in
    ' Solution Explorer), and make changes on the Application tab.
    '
    Partial Friend Class MyApplication

        <Global.System.Diagnostics.DebuggerStepThroughAttribute()>  _
        Public Sub New()
            MyBase.New(Global.Microsoft.VisualBasic.ApplicationServices.AuthenticationMode.Windows)
            Me.IsSingleInstance = false
            Me.EnableVisualStyles = true
            Me.SaveMySettingsOnExit = true
            Me.ShutDownStyle = Global.Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses
        End Sub

        <Global.System.Diagnostics.DebuggerStepThroughAttribute()>  _
        Protected Overrides Sub OnCreateMainForm()
            Me.MainForm = Global.AccountAndClientFull.frmMain 'HERE IS WHERE THE ERROR OCCURS
        End Sub
    End Class
End Namespace

Error occurs at:

Me.MainForm = Global.AccountAndClientFull.frmMain

Any suggestions? I can't figure out what changes need to be made in the application tab of the project properties.

Josh McKearin
  • 742
  • 4
  • 19
  • 42

9 Answers9

6

I got the same error and realized it was because I had declared a private default constructor in the form code. If you created a constructor and didn't make it public, try making it public.

chrismay
  • 1,384
  • 2
  • 15
  • 25
  • I was grooming some old code, switching from no-constructor and use of an explicit Load handler to having a constructor and OnLoad method, per this good SO discussion: http://stackoverflow.com/questions/2521322/what-setup-code-should-go-in-form-constructors-versus-form-load-event. I got this error when I add the constructor -- not sure if the IDE made it private or I did, but that was definitely the problem. – Anne Gunn Oct 30 '15 at 15:54
3

Make sure you have a Form Class called frmMain, attention not the file name but the Class Name.

Maybe you have renamed the Form file name to frmMain.vb but in the code of the file the signature of the class remains different.

Henry Rodriguez
  • 805
  • 7
  • 11
2

Another possible cause of this error is if an exception occours in the event handlers of the controls of the forms during initialization. The creation of the form fails and so an exception is raised.

Ste
  • 187
  • 3
  • 8
0

Another possible cause of this symptom, and a particularly obscure one:

I added two LineShape controls to a child form in Designer and it caused the Me.MainForm error.

(Toolbox/Visual Basic PowerPacks/LineShape).

This point in the documentation may provide a clue:

"When you create a LineShape control at run time, you must also create a ShapeContainer and set the Parent property of the LineShape to the ShapeContainer."

I added the LineShapes using designer so it should have created the ShapeContainers automatically but that may have failed, perhaps because I added them inside a container several layers deep already. I removed the LineShapes to resolve the issue.

RobinKay
  • 13
  • 4
0

If you have any criteria directly under the class that causes an error, it will also break on this error.
For example:

Public Class Form1
    Dim FILE_NAME As String = "C:\Folder\File.txt" '//if this file does not exist
    Dim objReader As New System.IO.StreamReader(FILE_NAME)

This last line will cause an error in Protected Overrides

MalaKa
  • 3,734
  • 2
  • 18
  • 31
0

I didn't like VB.NET feature when carriage return from string, so added this code under Public Class Form1 in hope to remove that annoying one:

Dim origString As String
Dim newString As String = origString.Replace(vbCr, "").Replace(vbLf, "")

This code caused a same problem. So possible solution is to comment or remove it:

'Dim origString As String 'From Multiline Textbox'
'Dim newString As String = origString.Replace(vbCr, "").Replace(vbLf, "")
0

I had the same problem... i deleted all the tools in my form and then it ran without any issue... then i found out that it was the webbrowser control that caused me the problem... i deleted it and added it again and the program ran without any trouble... try removing controls one by one and check which one causing the problem... then delete the particular control and re-add it to make it work... this happens when you copy your solution from one pc to another...

0
 Public Sub New()
        InitializeComponent()
 End Sub
  • JUST ADD THIS CODE TO YOUR FORM frmMain
  • Welcome to SO. See https://stackoverflow.com/help/how-to-answer. This is a very old question that already has an accepted answer. – Nick Jul 13 '18 at 14:34
0

I actually had this error when I ADDED the following to my Form.

    Private Sub New()
        InitializeComponent()
    End Sub

Very strange!

Chris Raisin
  • 384
  • 3
  • 7