In my VB.NET program i have greated, i am getting the above error with the following description

An error occurred creating the form. See Exception.InnerException for details. The error is: Object variable or With block variable not set.

The debugger doesnt point me towards any actual code to fix, so i am stumped.

How would I go about debugging this error?

Thanks.

(PS, I am learning VB.NET at the moment, the program i am making is an example from a book)

Dani AI

Generated

Short checklist and concrete ways to find the real error (expands on and ):

That InvalidOperationException with the text "An error occurred creating the form. See Exception.InnerException for details." is a wrapper thrown when a form fails during construction (constructor / InitializeComponent). The useful information is inside the InnerException — the trick is getting the debugger to stop at the place the inner exception is thrown so the offending line and stack trace are visible. (stackoverflow.com)

Recommended debugging workflow (fastest to slowest):

  1. Make the debugger stop at the actual throw: enable first‑chance/“Thrown” for CLR exceptions (older VS: Debug → Exceptions; newer VS: Debug → Windows → Exception Settings). This forces the debugger to break where the exception originates instead of only showing the wrapper. Also consider toggling Just My Code so the call stack shows framework/non-user frames if needed. (learn.microsoft.com)

  2. If the debugger still only shows the wrapper, temporarily wrap the form constructor/InitializeComponent call in a try/catch and log ex.ToString() (it prints InnerException and full stack). Remove this wrapper after debugging. Example:

Public Sub New()
    Try
        InitializeComponent()
    Catch ex As Exception
        MessageBox.Show(ex.ToString(), "Init error")
        Throw
    End Try
End Sub

This reveals the real exception text and stack trace so the broken line can be fixed. (stackoverflow.com)

Common culprits and quick isolation tips:

  • Field initializers that reference controls or resources (e.g., Dim s = TextBox1.Text) run before Load — move those to Form_Load. Custom controls or event handlers firing during initialization can throw. Missing/mismatched assemblies or COM/ActiveX registration problems also show up as InnerException — use the Assembly Binding Log Viewer (Fuslogvw) for bind failures and check x86/x64 platform targets for native/COM dependencies. Binary‑search by commenting half the initialization to narrow the line. (stackoverflow.com)

Summary note: combine ’s stepping and ’s breakpoints with the first‑chance exception setting and the InitializeComponent try/catch above — that combination almost always exposes the InnerException and the exact line to fix.

Recommended Answers

All 4 Replies

Single step thru the load. Most of the time you are referencing something that hasn't been loaded. Such as setting a check box but it hasn't been created yet.

Single step thru the load. Most of the time you are referencing something that hasn't been loaded.

How would I do this?

I am still learning, I thought the debugger should tell me where the error is.

You would expect the debuger to tell you that but it doesn't. Press the F8 key to single step thru the code. You will run into a line where you will get an error message.

or may be you can use BreakPoints to find the Error. On your form_load event on the left of your screen set a breakpoint(refer the book how breakpoint works) and Press F9 or F10 to cycle through your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.