对象引用未设置为对象的实例...创建表单时 [英] Object Reference not Set To An Instance Of An Object...While Creating a form

查看:111
本文介绍了对象引用未设置为对象的实例...创建表单时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个程序(代码片段管理器),我正在尝试。现在,我在创建表单时遇到错误,我不知道它出错的地方。有人可以帮助我,告诉我哪里出错了吗?这是代码。

  Imports  System.ComponentModel 

公开 Form1
私有 Const WM_NCLBUTTONDOWN 作为 =& HA1
私有 Const WM_NCLBUTTONUP As =& HA0
私有 Const WM_MOVING 作为 =& H216
私有 Const WM_SIZE 作为 = & H5
受保护的 覆盖 Sub DefWndProc( ByRef m As System.Windows.Forms.Message)
静态 LButtonDown < span class =code-keyword> As Boolean
如果 CLng (m.Msg)= WM_NCLBUTTONDOWN 然后
LButtonDown = True
ElseIf CLng (m.Msg)= WM_NCLBUTTONUP 然后
LButtonDown = 错误
结束 如果
如果 LButtonDown n
如果 CLng (m.Msg)= WM_MOVING 然后
如果 .Opacity<> ; 0 9 然后 Me .Opacity = 0 5
< span class =code-keyword> ElseIf CLng (m.Msg)= WM_SIZE 然后
如果 .Opacity<> 0 6 然后 Me .Opacity = 0 6
< span class =code-keyword>结束 如果
ElseIf LButtonDown 然后
如果 Me .Opacity<> 1 0 然后 Me .Opacity = 1 0
< span class =code-keyword>结束 如果
MyBase .DefWndProc (m)
结束 Sub
公共 SnippetsFolder 作为 字符串 = Application.StartupPath& \ Snippets \
公开 RelativeFolder As Boolean = IO.File.ReadAllLines(Application.StartupPath& \ Settingtings.cdbs)( 0
公开 AutoSave 作为 布尔 = IO.File.ReadAllLines(Application.StartupPath& \ Settings.cdbs)(< span class =code-digit> 2 )
私有 Sub MenuItem3_Click (发件人作为 对象,e 作为 EventArgs )句柄 MenuItem3.Click
FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.CSharp
End Sub

私有 Sub MenuItem4_Click(发件人 As Object ,e As EventArgs)句柄 MenuItem4.Click
FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.VB
结束 Sub

私人 Sub MenuItem5_Click(发件人) 作为 对象,e 作为 EventArgs)< span class =code-keyword>句柄 MenuItem5.Click
FastColoredTextBox1.Langu age = FastColoredTextBoxNS.Language.HTML
结束 Sub

< span class =code-keyword>私有 Sub MenuItem6_Click(发件人作为 对象,e As EventArgs)句柄 MenuItem6.Click
FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.XML
结束 Sub

私有 Sub MenuItem7_Click(发件人 As 对象,e As EventArgs)句柄 MenuItem7.Click
FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.SQL
结束 Sub

私人 Sub MenuItem8_Click(发件人作为 对象,e As EventArgs)句柄 MenuItem8.Click
FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.PHP
结束 Sub

私有 Sub MenuItem9_Click(发件人作为 对象,e As EventArgs)句柄 MenuItem9.Click
FastColoredTextBox1.Language = FastColoredTextBoxNS.Language。 JS
结束 Sub

私有 Sub MenuItem10_Click(发件人 As 对象,e 作为 EventArgs)句柄 MenuItem10.Click
FastColoredTextBox1.Language = FastColoredTextBoxNS.Language.Lua
End Sub
私有 Sub Form1_Load(sender As 对象,e As EventArgs)句柄 MyBase .Load
' Dim lsc As New launchScreenControl
' Me.Controls.Add(l sc)
' lsc.Dock = DockStyle.Fill
' lsc.BringToFront()
' 如果RelativeFolder = False那么
' SnippetsFolder = IO.File.ReadAllLines(Application.StartupPath& \ SETtings.cdbs)(1)& \
其他:SnippetsFolder = Application.StartupPath& \ Snippets \
' 结束时如果
' RebuildSnippetList()
结束 Sub

私有 Sub MenuItem11_Click(发件人作为 对象,e 作为 EventArgs)句柄 MenuItem11.Click
NewSnippet()
结束 Sub
Sub NewSnippet()
IO.File.WriteAllText(SnippetsFolder& InputBox( 请为该片段命名:)& 。cdbf
RebuildSnippetList()
结束 Sub
Sub RebuildSnippetList()
Dim MyImages As ImageList()
MyImages.ColorDepth = ColorDepth.Depth32Bit
MyImages.ImageSize = 大小( 16 16
MyImages.Images.Add( 文件夹,My.Resources.winvista_ico_shell32_dll_003)
MyImages.Images.Add( Item,My.Resources.winvista_ico_shell32_dll_071)
TreeView1.ImageList = MyImages
TreeVie w1.Nodes.Clear()
如果 IO.Directory.Exists(SnippetsFolder)= False 然后 IO.Directory.CreateDirectory(SnippetsFolder)
Dim i 作为 整数 = 0
对于 每个文件夹作为 字符串 IO.Directory.GetDirectories(SnippetsFolder)
TreeView1.Nodes.Add( 文件夹,My.Computer.FileSystem.GetName(文件夹))
对于 < span class =code-keyword>每个文件作为 字符串 IO.Directory.GetFiles(文件夹, * .cdbf
Dim specFile As String = My.Computer.FileSystem.GetName(File)
Dim specIndex As 整数 = specFile.LastIndexOf(
TreeView1.Nodes(i).Nodes.Add( Item,specFile.Remove( specIndex), 1 1
下一步
i + = 1
下一步
' ListBox1.Items.Clear()
' 如果是IO.Directory。 Exists(SnippetsFolder)= False然后IO.Directory.CreateDirectory(SnippetsFolder)
' For Each file As String在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))
' 下一步
结束 Sub

私人 Sub MenuItem17_Click(发件人 As Object ,e As EventArgs) Handles MenuItem17.Click
< span class =code-keyword> Me .Close()
结束 Sub

私有 Sub NewToolStripButton_Click(发件人作为 对象,e 作为 EventArgs)句柄 NewToolStripButton.Click
NewSnippet()
结束 Sub

私有 Sub CutToolStripButton_Click(sender 作为 对象,e 作为 EventArgs)句柄 CutToolStripButton.Click
FastColoredTextBox1.Cut()
结束 Sub

私有 Sub CopyToolStripButton_Click(sender As Object ,e As EventArgs)句柄 CopyToolStripButton.Click
FastColoredTextBox1.Copy()
结束 Sub

私有 Sub PasteToolStripButton_Click(发件人作为 对象,e 作为 EventArgs)句柄 PasteToolStripButton.Click
FastColoredTextBox1.Paste()
结束 Sub
Sub SingleImport()
Dim ofd As OpenFileDialog
ofd.Filter = 编码器的Digi-Book文件(* .cdbf)| * .cdbf
ofd.Title = 导入代码段。
如果 ofd.ShowDialog = Windows.Forms.DialogResult.OK 然后
如果 IO.Directory.Exists (SnippetsFolder& \Imported \)= False 然后 IO.Directory.CreateDirectory(SnippetsFolder& \Imported \
IO.File.Copy(ofd.FileName,SnippetsFolder& \Imported \& My.Computer.FileSystem.GetName(ofd.FileName))
RebuildSnippetList()
结束 如果
结束 Sub
私有 Sub OpenToolStripButton_Click(发件人作为 对象,e As EventArgs)句柄 OpenToolStripButton.Click
SingleImport()
结束 Sub

私有 Sub MenuItem12_Click(发件人作为 对象,e 作为 EventArgs)句柄 MenuItem12.Click
SingleImport()
结束 Sub
Sub SingleExport()
如果 TreeView1.SelectedNode.SelectedImageIndex = 1 然后
Dim sfd 作为 SaveFileDialog
sfd.Filter = 编码器的Digi-Book文件(* .cdbf)| * .cdbf
sfd.Title = < span class =code-string> Export Snippet ..
如果 sfd.ShowDialog = Windows.Forms.DialogResult.OK 然后
IO.File.Copy(SnippetsFolder& \& TreeView1.SelectedNode.Parent.Text& \& TreeView1.SelectedNode.Text& 。cdbf,sfd.FileName)
结束 如果
结束 如果
结束 Sub
私有 Sub MenuItem14_Click(发件人作为 对象,e As EventArgs)句柄 MenuItem14.Click
SingleExport()
结束 Sub

私有 Sub SaveToolStripButton_Click(发件人作为 对象,e As EventArgs)句柄 SaveToolStripButton.Click
If TreeView1.SelectedNode .SelectedImageIndex = 1 然后
IO.File.WriteAllText(SnippetsFolder& \& TreeView1.SelectedNode.Parent.Text& \& TreeView1.SelectedNode.Text& 。cdbf,FastColoredTextBox1.Text)
结束 如果
结束 Sub

私有 Sub ListBox1_SelectedIndexChanged(sender 作为 对象,e As EventArgs)句柄 ListBox1.SelectedIndexChanged
FastColoredTextBox1.Text = IO.File.ReadAllText(SnippetsFolder& ListBox1.SelectedItem& 。cdbf
结束 Sub

私有 Sub FastColoredTextBox1_TextChang ed(发件人作为 对象,e 作为 FastColoredTextBoxNS.TextChangedEventArgs)句柄 FastColoredTextBox1.TextChanged
如果 TreeView1.SelectedNode.ImageIndex = 1 然后
如果 AutoSave 然后 IO.File.WriteAllText(SnippetsFolder& \& TreeView1.SelectedNode.Parent.Text& \& TreeView1.SelectedNode.Text& 。cdbf,FastColoredTextBox1.Text)
结束 如果
结束 Sub

私有 Sub MenuItem20_Click(发件人作为 对象,e As EventArgs)句柄 MenuItem20.Click
RebuildSnippetList()
结束 Sub

私有 Sub HelpToolStripButton_Click(发件人 As Object ,e As EventArgs)句柄 H. elpToolStripButton.Click
Dim frm As New 设置
如果 frm.ShowDialog = Windows.Forms.DialogResult.OK 那么
Dim lbl 作为 标签
lbl.Text = 已对设置进行了更改。单击此文本以重新启动。
lbl.AutoSize = False
lbl.Font = 字体( Arial 12
lbl.Dock = DockStyle.Fill
AddHandler (lbl.Click), AddressOf ClickTheLabel
Me .Controls.Add(lbl)
lbl.BringToFront()
ToolStrip1。已启用= 错误
对于 每个 mnuItem 作为 MenuItem MainMenu1.MenuItems
mnuItem.Enabled = 错误
下一步
结束 如果
结束 < span class =code-keyword> Sub
Sub ClickTheLabel(sender As 对象,e As EventArgs)
Application.Restart()
结束 Sub

私有 Sub Form1_Resize(发件人作为 对象, e As EventArgs)句柄 .Resize
结束 Sub

私人 Sub MenuItem 13_Click(发件人作为 对象,e 作为 EventArgs)句柄 MenuItem13.Click
Dim FOLDERDG 正如 FolderBrowserDialog
FOLDERDG.ShowNewFolderButton = False
FOLDERDG。 Description = 请选择一个文件夹以从中检索片段。
如果 FOLDERDG.ShowDialog = Windows.Forms.DialogResult.OK 那么
对于 每个文件作为 字符串 IO.Directory.GetFiles(FOLDERDG.SelectedPath, * .cdbf
IO.File.Copy(file,SnippetsFolder& \Imported \& My.Computer.FileSystem.GetName(file))
下一步
结束 < span class =code-keyword>如果
RebuildSnippetList()
结束 Sub

私有 Sub MenuItem21_Click(发件人作为 对象,e As EventArgs)句柄 MenuItem21.Click
Dim FOLDERDG As FolderBrowserDialog
FOLDERDG.ShowNewFolderButton = False
FOLDERDG.Description = 请选择要将代码段复制到的文件夹。
如果 FOLDERDG.ShowDialog = Windows.Forms.DialogResult.OK 那么
对于 每个文件 As String IO.Directory.GetFiles(SnippetsFolder, * .cdbf
IO.File.Copy(file,FOLDERDG.SelectedPath& My.Computer.FileSystem.GetName(file))
下一步
结束 < span class =code-keyword>如果
结束 Sub

私有 Sub MenuItem23_Click(发件人 As 对象,e As EventArgs)句柄 MenuItem23.Click
Dim cde As codingEnvironment
cde.Show()
结束 Sub

私有 Sub TreeView1_AfterSelect(sender 作为 对象,e As TreeViewEventArgs)句柄 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



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[] Ar gs) 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\Docu ments\Visual Studio 2013\Projects\Coders Digi-Book\Coders Digi-Book\Form1.vb:line 28

InnerException:

解决方案

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.这是检测和修复的最简单的案例之一。它只是意味着某些引用类型的某个成员/变量通过使用和它的实例(非静态)成员解除引用,这要求此成员/变量为非null,但实际上它似乎为null。只需在调试器下执行它,它将停止抛出异常的执行。在该行上设置一个断点,重新启动应用程序并再次到达这一点。评估下一行中涉及的所有引用,并查看哪一个为null,而不需要为null。解决这个问题之后,修复代码:要么确保将成员/变量正确初始化为非空引用,要么将其检查为null,如果为null,则执行其他操作。



另请参阅:想要在按钮点击时显示下一条记录。但是如果下一个记录功能的条件对象引用未设置为对象的实例则会出错。



有时候,你不能这样做在调试器下,由一个或另一个原因。一个非常讨厌的情况是,只有在调试信息不​​可用时构建软件时才会出现问题。在这种情况下,你必须使用更难的方式。首先,你需要确保你永远不会通过静默处理异常来阻止异常的传播(这是开发人员对自己的犯罪,但很常见)。您需要在每个线程的最顶层堆栈帧上捕获绝对所有异常。如果处理类型 System.Exception 的异常,则可以执行此操作。在处理程序中,您需要记录所有异常信息,尤其是 System.Exception.StackTrace

http://msdn.microsoft.com/en-us/library/system.exception.aspx

http://msdn.microsoft.com/en-us/library/ system.exception.stacktrace.aspx



堆栈跟踪只是一个字符串,显示从throw语句到处理程序的异常传播的完整路径。通过阅读,您总能找到目的。对于日志记录,使用类 System.Diagnostics.EventLog

http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx



祝你好运,

-SA


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.

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:

解决方案

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


这篇关于对象引用未设置为对象的实例...创建表单时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆