Results 1 to 4 of 4

Thread: [RESOLVED] An error occurred creating the form

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Resolved [RESOLVED] An error occurred creating the form

    This has happened on the last 3 projects i created.

    All projects only had a textbox and button with basic code.

    If i comment out all code in the form, project runs.


    Application.Designer.vb
    Code:
    '------------------------------------------------------------------------------
    ' <auto-generated>
    '     This code was generated by a tool.
    '     Runtime Version:4.0.30319.42000
    '
    '     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.NewClientConsole.Form1 'ERROR HERE
            End Sub
        End Class
    End Namespace
    ERROR
    System.InvalidOperationException
    HResult=0x80131509
    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.
    Source=NewClientConsole
    StackTrace:
    at NewClientConsole.My.MyProject.MyForms.Create__Instance__[T](T Instance) in :line 190
    at NewClientConsole.My.MyProject.MyForms.get_Form1()
    at NewClientConsole.My.MyApplication.OnCreateMainForm() in C:\Users\mcapp\OneDrive\Documents\Visual Studio 2017\NewClientConsole\NewClientConsole\My Project\Application.Designer.vb:line 35
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    at NewClientConsole.My.MyApplication.Main(String[] Args) in :line 81

    Inner Exception 1:
    NullReferenceException: Object reference not set to an instance of an object.

  2. #2

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    [RESOLVED]Re: An error occurred creating the form

    opps, had this , sorry

    so ClientName was expecting to have a value which looks like its the problem.
    Code:
    Public Class Form2
        Dim ClientName As String = TxtName.Text

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,632

    Re: An error occurred creating the form

    You are almost certainly running in to an issue where TxtName doesn't yet exist when that line of code is being executed.

    Change it to:

    Code:
    Dim ClientName As String
    Then, in the Form_Load event, add:

    Code:
    ClientName = TxtName.Text

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: An error occurred creating the form

    Quote Originally Posted by OptionBase1 View Post
    Change it to:

    Code:
    Dim ClientName As String
    Really ought to be:
    Code:
    Private ClientName As String
    Always better to be explicit about access level.
    Quote Originally Posted by OptionBase1 View Post
    Then, in the Form_Load event, add:

    Code:
    ClientName = TxtName.Text
    That would accomplish what the original code appears to intend, but is that worthwhile anyway? What's the point in getting the Text from a TextBox that the user hasn't even seen yet, thus could not have populated yet? If that TextBox is populated via a bound setting or the like, then the field should be populated from that setting rather than from the control.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width