Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
I have an error when creating my form, and this is what it says. I have no idea what to do. Could someone help?


Thanks,
iProgramIt

The error:
System.InvalidOperationException was unhandled
  HResult=-2146233079
  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=Coders Digi-Book
  StackTrace:
       at Coders_Digi_Book.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
       at Coders_Digi_Book.My.MyProject.MyForms.get_Form1()
       at Coders_Digi_Book.My.MyApplication.OnCreateMainForm() in C:\Users\mac0017\Documents\Visual Studio 2013\Projects\Coders Digi-Book\Coders Digi-Book\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 Coders_Digi_Book.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.NullReferenceException
       HResult=-2147467261
       Message=Object reference not set to an instance of an object.
       Source=Coders Digi-Book
       StackTrace:
            at Coders_Digi_Book.Form1.FastColoredTextBox1_TextChanged(Object sender, TextChangedEventArgs e) in C:\Users\mac0017\Documents\Visual Studio 2013\Projects\Coders Digi-Book\Coders Digi-Book\Form1.vb:line 157
            at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
            at FastColoredTextBoxNS.FastColoredTextBox.OnTextChanged(TextChangedEventArgs args)
            at FastColoredTextBoxNS.FastColoredTextBox.OnTextChanged()
            at FastColoredTextBoxNS.FastColoredTextBox.System.ComponentModel.ISupportInitialize.EndInit()
            at Coders_Digi_Book.Form1.InitializeComponent() in C:\Users\mac0017\Documents\Visual Studio 2013\Projects\Coders Digi-Book\Coders Digi-Book\Form1.Designer.vb:line 360
            at Coders_Digi_Book.Form1..ctor() in C:\Users\mac0017\Documents\Visual Studio 2013\Projects\Coders Digi-Book\Coders Digi-Book\Form1.vb:line 28
       InnerException: 


The Form's code:
VB
Imports System.ComponentModel

Public Class Form1
    Private Const WM_NCLBUTTONDOWN As Long = &HA1
    Private Const WM_NCLBUTTONUP As Long = &HA0
    Private Const WM_MOVING As Long = &H216
    Private Const WM_SIZE As Long = &H5
    Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
        Static LButtonDown As Boolean
        If CLng(m.Msg) = WM_NCLBUTTONDOWN Then
            LButtonDown = True
        ElseIf CLng(m.Msg) = WM_NCLBUTTONUP Then
            LButtonDown = False
        End If
        If LButtonDown Then
            If CLng(m.Msg) = WM_MOVING Then
                If Me.Opacity <> 0.9 Then Me.Opacity = 0.5
            ElseIf CLng(m.Msg) = WM_SIZE Then
                If Me.Opacity <> 0.6 Then Me.Opacity = 0.6
            End If
        ElseIf Not LButtonDown Then
            If Me.Opacity <> 1.0 Then Me.Opacity = 1.0
        End If
        MyBase.DefWndProc(m)
    End Sub
    Public SnippetsFolder As String = Application.StartupPath & "\Snippets\"
    Public RelativeFolder As Boolean = IO.File.ReadAllLines(Application.StartupPath & "\Settings.cdbs")(0)
    Public AutoSave As Boolean = IO.File.ReadAllLines(Application.StartupPath & "\Settings.cdbs")(2)
    Private Sub MenuItem3_Click(sender As Object, e As EventArgs) Handles MenuItem3.Click
        FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.CSharp
    End Sub
    Private Sub MenuItem4_Click(sender As Object, e As EventArgs) Handles MenuItem4.Click
        FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.VB
    End Sub
    Private Sub MenuItem5_Click(sender As Object, e As EventArgs) Handles MenuItem5.Click
        FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.HTML
    End Sub
    Private Sub MenuItem6_Click(sender As Object, e As EventArgs) Handles MenuItem6.Click
        FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.XML
    End Sub
    Private Sub MenuItem7_Click(sender As Object, e As EventArgs) Handles MenuItem7.Click
        FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.SQL
    End Sub
    Private Sub MenuItem8_Click(sender As Object, e As EventArgs) Handles MenuItem8.Click
        FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.PHP
    End Sub
    Private Sub MenuItem9_Click(sender As Object, e As EventArgs) Handles MenuItem9.Click
        FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.JS
    End Sub
    Private Sub MenuItem10_Click(sender As Object, e As EventArgs) Handles MenuItem10.Click
        FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.Lua
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'this goes into the event code  
        If Me.IsInitializing = True Then
            Exit Sub
        Else
            'place the code for the event here  
        End If
        Dim lsc As New launchScreenControl
        Me.Controls.Add(lsc)
        lsc.Dock = DockStyle.Fill
        lsc.BringToFront()
        If RelativeFolder = False Then
            SnippetsFolder = IO.File.ReadAllLines(Application.StartupPath & "\Settings.cdbs")(1) & "\"
        Else : SnippetsFolder = Application.StartupPath & "\Snippets\"
        End If
        RebuildSnippetList()
    End Sub
    Private Sub MenuItem11_Click(sender As Object, e As EventArgs) Handles MenuItem11.Click
        NewSnippet()
    End Sub
    Sub NewSnippet()
        IO.File.WriteAllText(SnippetsFolder & InputBox("Please name the snippet:") & ".cdbf", "")
        RebuildSnippetList()
    End Sub
    Sub RebuildSnippetList()
        Dim MyImages As New ImageList()
        MyImages.ColorDepth = ColorDepth.Depth32Bit
        MyImages.ImageSize = New Size(16, 16)
        MyImages.Images.Add("Folder", My.Resources.winvista_ico_shell32_dll_003)
        MyImages.Images.Add("Item", My.Resources.winvista_ico_shell32_dll_071)
        TreeView1.ImageList = MyImages
        TreeView1.Nodes.Clear()
        If IO.Directory.Exists(SnippetsFolder) = False Then IO.Directory.CreateDirectory(SnippetsFolder)
        Dim i As Integer = 0
        For Each Folder As String In IO.Directory.GetDirectories(SnippetsFolder)
            TreeView1.Nodes.Add("Folder", My.Computer.FileSystem.GetName(Folder))
            For Each File As String In IO.Directory.GetFiles(Folder, "*.cdbf")
                Dim specFile As String = My.Computer.FileSystem.GetName(File)
                Dim specIndex As Integer = specFile.LastIndexOf(".")
                TreeView1.Nodes(i).Nodes.Add("Item", specFile.Remove(specIndex), 1, 1)
            Next
            i += 1
        Next
        'ListBox1.Items.Clear()
        'If IO.Directory.Exists(SnippetsFolder) = False Then IO.Directory.CreateDirectory(SnippetsFolder)
        'For Each file As String In IO.Directory.GetFiles(SnippetsFolder, "*.cdbf")
        '    Dim specFile As String = My.Computer.FileSystem.GetName(file)
        '    Dim specIndex As Integer = specFile.LastIndexOf(".")
        '    ListBox1.Items.Add(specFile.Remove(specIndex))
        'Next
    End Sub
    Private Sub MenuItem17_Click(sender As Object, e As EventArgs) Handles MenuItem17.Click
        Me.Close()
    End Sub
    Private Sub NewToolStripButton_Click(sender As Object, e As EventArgs) Handles NewToolStripButton.Click
        NewSnippet()
    End Sub
    Private Sub CutToolStripButton_Click(sender As Object, e As EventArgs) Handles CutToolStripButton.Click
        FastColoredTextBox1.Cut()
    End Sub
    Private Sub CopyToolStripButton_Click(sender As Object, e As EventArgs) Handles CopyToolStripButton.Click
        FastColoredTextBox1.Copy()
    End Sub
    Private Sub PasteToolStripButton_Click(sender As Object, e As EventArgs) Handles PasteToolStripButton.Click
        FastColoredTextBox1.Paste()
    End Sub
    Sub SingleImport()
        Dim ofd As New OpenFileDialog
        ofd.Filter = "Coders' Digi-Book Files (*.cdbf)|*.cdbf"
        ofd.Title = "Import Snippet.."
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            If IO.Directory.Exists(SnippetsFolder & "\Imported\") = False Then IO.Directory.CreateDirectory(SnippetsFolder & "\Imported\")
            IO.File.Copy(ofd.FileName, SnippetsFolder & "\Imported\" & My.Computer.FileSystem.GetName(ofd.FileName))
            RebuildSnippetList()
        End If
    End Sub
    Private Sub OpenToolStripButton_Click(sender As Object, e As EventArgs) Handles OpenToolStripButton.Click
        SingleImport()
    End Sub
    Private Sub MenuItem12_Click(sender As Object, e As EventArgs) Handles MenuItem12.Click
        SingleImport()
    End Sub
    Sub SingleExport()
        If TreeView1.SelectedNode.SelectedImageIndex = 1 Then
            Dim sfd As New SaveFileDialog
            sfd.Filter = "Coders' Digi-Book Files (*.cdbf)|*.cdbf"
            sfd.Title = "Export Snippet.."
            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                IO.File.Copy(SnippetsFolder & "\" & TreeView1.SelectedNode.Parent.Text & "\" & TreeView1.SelectedNode.Text & ".cdbf", sfd.FileName)
            End If
        End If
    End Sub
    Private Sub MenuItem14_Click(sender As Object, e As EventArgs) Handles MenuItem14.Click
        SingleExport()
    End Sub
    Private Sub SaveToolStripButton_Click(sender As Object, e As EventArgs) Handles SaveToolStripButton.Click
        If TreeView1.SelectedNode.SelectedImageIndex = 1 Then
            IO.File.WriteAllText(SnippetsFolder & "\" & TreeView1.SelectedNode.Parent.Text & "\" & TreeView1.SelectedNode.Text & ".cdbf", FastColoredTextBox1.Text)
        End If
    End Sub
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        FastColoredTextBox1.Text = IO.File.ReadAllText(SnippetsFolder & ListBox1.SelectedItem & ".cdbf")
    End Sub
    Private Sub FastColoredTextBox1_TextChanged(sender As Object, e As FastColoredTextBoxNS.TextChangedEventArgs) Handles FastColoredTextBox1.TextChanged
        If TreeView1.SelectedNode.ImageIndex = 1 Then
            If AutoSave Then IO.File.WriteAllText(SnippetsFolder & "\" & TreeView1.SelectedNode.Parent.Text & "\" & TreeView1.SelectedNode.Text & ".cdbf", FastColoredTextBox1.Text)
        End If
    End Sub
    Private Sub MenuItem20_Click(sender As Object, e As EventArgs) Handles MenuItem20.Click
        RebuildSnippetList()
    End Sub
    Private Sub HelpToolStripButton_Click(sender As Object, e As EventArgs) Handles HelpToolStripButton.Click
        Dim frm As New Settings
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim lbl As New Label
            lbl.Text = "Changes have been made to settings. Click this text to restart."
            lbl.AutoSize = False
            lbl.Font = New Font("Arial", 12)
            lbl.Dock = DockStyle.Fill
            AddHandler (lbl.Click), AddressOf ClickTheLabel
            Me.Controls.Add(lbl)
            lbl.BringToFront()
            ToolStrip1.Enabled = False
            For Each mnuItem As MenuItem In MainMenu1.MenuItems
                mnuItem.Enabled = False
            Next
        End If
    End Sub
    Sub ClickTheLabel(sender As Object, e As EventArgs)
        Application.Restart()
    End Sub
    Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
    End Sub
    Private Sub MenuItem13_Click(sender As Object, e As EventArgs) Handles MenuItem13.Click
        Dim FOLDERDG As New FolderBrowserDialog
        FOLDERDG.ShowNewFolderButton = False
        FOLDERDG.Description = "Please select a folder to retrieve the snippets from."
        If FOLDERDG.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each file As String In IO.Directory.GetFiles(FOLDERDG.SelectedPath, "*.cdbf")
                IO.File.Copy(file, SnippetsFolder & "\Imported\" & My.Computer.FileSystem.GetName(file))
            Next
        End If
        RebuildSnippetList()
    End Sub
    Private Sub MenuItem21_Click(sender As Object, e As EventArgs) Handles MenuItem21.Click
        Dim FOLDERDG As New FolderBrowserDialog
        FOLDERDG.ShowNewFolderButton = False
        FOLDERDG.Description = "Please select a folder to copy the snippets to."
        If FOLDERDG.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each file As String In IO.Directory.GetFiles(SnippetsFolder, "*.cdbf")
                IO.File.Copy(file, FOLDERDG.SelectedPath & My.Computer.FileSystem.GetName(file))
            Next
        End If
    End Sub
    Private Sub MenuItem23_Click(sender As Object, e As EventArgs) Handles MenuItem23.Click
        Dim cde As New codingEnvironment
        cde.Show()
    End Sub
    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect
        If TreeView1.SelectedNode.SelectedImageIndex = 1 Then
            FastColoredTextBox1.Enabled = True
            FastColoredTextBox1.Text = IO.File.ReadAllText(SnippetsFolder & "\" & e.Node.Parent.Text & "\" & e.Node.Text & ".cdbf")
        Else
            FastColoredTextBox1.Enabled = False
            FastColoredTextBox1.Text = ""
        End If
    End Sub
    Private Sub FastColoredTextBox1_Load(sender As Object, e As EventArgs) Handles FastColoredTextBox1.Load

    End Sub
End Class
Posted

1 solution

Does the file "Settings.cdbs" definitely exist in the application folder? (Remember this will be in bin\Debug or bin\Release when you build your app from the IDE)

If not, select the file in the IDE and in the properties pane set "Copy to output" property to "Always".
 
Share this answer
 
Comments
iProgramIt 27-Oct-15 18:40pm    
Yes it does exist. In both folders.
Duncan Edwards Jones 28-Oct-15 3:37am    
You need to set a breakpoint on "Public SnippetsFolder As String = Application.StartupPath & "\Snippets\"" and step through to see what line the code stops on
iProgramIt 30-Oct-15 2:00am    
Hey, I found the issue. The Textbox was causing the issue. Thanks anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900