Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi guys,
I have a program (code snippet manager) that I am trying to make. Now, I am getting an error when the form is created, and I don't know where it is going wrong. Can someone help me and tell where I have gone wrong? Here's the 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
        '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


Thanks in Advance,
iProgramIt

EDIT: See my comment about the form is not loaded. The following is 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 168
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 359
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:
Posted
Updated 26-Oct-15 19:37pm
v2
Comments
Patrice T 26-Oct-15 23:01pm    
When you get the error message, you are also told where is the problem.
Sergey Alexandrovich Kryukov 26-Oct-15 23:09pm    
In what line?
—SA
iProgramIt 26-Oct-15 23:11pm    
It doesn't say. Just creating the form
Sergey Alexandrovich Kryukov 26-Oct-15 23:46pm    
What "it"? You should say that, not "it". Use the debugger, and so on — read my answer.
—SA
iProgramIt 27-Oct-15 0:17am    
No, the form is not even created - the program starts, and then it gives me an error.
I must admit, I don't understand this error report. Sorry.
Thank you,
iProgramIt

1 solution

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object".

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx,
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx.

Good luck,
—SA
 
Share this answer
 

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