[RESOLVED] InitializeComponent Error
I have been fixing up code that I wrote years ago before I knew what I was doing. I have mostly just changed syntax for variables and changed many Public functions to Friend.
After completed this maintenance I now get a "object reference not set to instance of an object" error when I try to open a form at the InitializeComponent line of it's New sub. This doesn't happen at start up - only when I click a button to open a different form.
I must have changed something I shouldn't have. I get this error whenever I forget to use the new keyword but I'm not sure how this applies to InitializeComponent().
I copied the Call Stack which I really have never deciphered before so go easy on me please. :) Thanks in advance.
Code:
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=DynastyFootball
StackTrace:
at DynastyFootball.frmPlayDesigner.InitializeComponent() in C:\DynastyFootball\DynastyFootball\frmPlayDesigner.Designer.vb:line 725
at DynastyFootball.frmPlayDesigner..ctor(ClLeague League, UCPlayerCard PlayerCard, frmMainMenu MainMenu, CLPlay Play) in C:\DynastyFootball\DynastyFootball\frmPlayDesigner.vb:line 114
at DynastyFootball.frmMainMenu.DisplayPlayDesignerScreen(CLPlay Play) in C:\DynastyFootball\DynastyFootball\frmMainMenu.vb:line 2729
at DynastyFootball.UCPlaybook.btNewPlay_Click(Object sender, EventArgs e) in C:\DynastyFootball\DynastyFootball\UCPlaybook.vb:line 191
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at DynastyFootball.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Re: InitializeComponent Error
Greetings Neff, have you attempted to run thru the offending code using the debugger line by line and if so what line is failing. Best to explain brief details and a small snippet of code too when replying.
Lastly what are your settings for Option Strict, Option Infer and what Framework are you targetting.
Re: InitializeComponent Error
The line that stops the debugger is the InitializeComponent call. I never actually get into that sub (created by the VB.Net's designer). It gets through all the private variables and constants I declared, then returns to New and then whether I press F10 or F11 the error message appears.
Code:
Public Sub New(ByVal League As ClLeague, ByVal PlayerCard As UCPlayerCard, ByVal MainMenu As frmMainMenu, ByVal Play As CLPlay)
' This call is required by the Windows Form Designer.
InitializeComponent() 'Error is here
Dim height As Integer = My.Computer.Screen.Bounds.Height
Dim width As Integer = My.Computer.Screen.Bounds.Width
etc...
I use the professional version of Visual Studio 2010. Option Infer is On, Option Strict is off.
Re: InitializeComponent Error
Rather than ask a lot of questions try this.
With the project selected in the IDE select menu item project then show all files. Next open the form designer file and look for
Code:
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Comment out the debugger step thru attruibute
Code:
'<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Place a break-point on the first line in the procedure i.e. Place a break point on Me.Panel1 ...
Code:
'<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Panel1 = New System.Windows.Forms.Panel
Now rebuilt the project and step thru the procedure.
Some things that might arise, issues (if you have one) with a non standard control (perhaps from a third party library or VB6 control) not compatible with VS2010. A property that does not exists anymore etc.
When done uncomment the line
Code:
<System.Diagnostics.DebuggerStepThrough()> _
I am going to assume you will hit a problem doing the above.
Re: InitializeComponent Error
Thanks so much, I found the problem.
Quite a while ago (years) I created my own inherited Picturebox class that automatically set parameters for DoubleBuffering. I created this class in a module and never properly incorporated it into my project (as a DLL file). I have had problems loading the form's designer ever since (constantly rebuilding until for some reason it decides to work). I'm not sure why this error came up now, perhaps because of my house cleaning (it was exactly the same in my backup file that worked fine for years).
There was no new declaration in the designer. Only this line (pbField is the PictureBox):
Code:
CType(Me.pbField, System.ComponentModel.ISupportInitialize).BeginInit()
I added this line right before to get it working, at least for now:
Code:
Me.pbField = New DoubleBufferPictureBox
Does my newer version of VS now have double-buffering as a standard option in the design window for pictureboxes? I'd love to scrap this horrible code - that was the entire point of cleaning house: to get things right that I didn't understand (or got bad advice about). I'd either like to learn how to create a real DLL file or just just set these Picturebox parameters in a better place than a module:
Code:
SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.ResizeRedraw, True)
Me.UpdateStyles()
Also, could you explain what System.Diagnostics.DebuggerStepThrough does? If it wasn't there in the first place I would have caught this error in 30 seconds. It must have some grander purpose.
This problem caused some anxiety so thanks so much for helping me. :)
-neef
Re: InitializeComponent Error
DebuggerStepThroughAttribute Class
Instructs the debugger to step through the code instead of stepping into the code. This class cannot be inherited.
You can use this in your code also, for instance when debugging something you do not want to step into known working code so you use DebuggerStepThrough.
Example code that I do not care to step into
Code:
Namespace My
<Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never)> _
Partial Friend Class _Dialogs
<System.Diagnostics.DebuggerStepThrough()> _
Public Function Question(ByVal Text As String) As Boolean
Return (MessageBox.Show(Text, _
My.Application.Info.Title, _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
End Function
End Class
<Global.Microsoft.VisualBasic.HideModuleName()> _
Friend Module KSG_Dialogs
Private instance As New ThreadSafeObjectProvider(Of _Dialogs)
ReadOnly Property Dialogs() As _Dialogs
Get
Return instance.GetInstance()
End Get
End Property
End Module
End Namespace