问题
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.
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
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
回答6:
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, "")
回答7:
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...
回答8:
Public Sub New()
InitializeComponent()
End Sub
- JUST ADD THIS CODE TO YOUR FORM frmMain
来源:https://stackoverflow.com/questions/11887232/error-inside-of-application-designer-vb-inside-of-oncreatemainform-sub