空引用异常错误 [英] Null reference exception error

查看:101
本文介绍了空引用异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次运行程序时都会出现空引用异常错误。我该怎么办呢?这里是代码:



I get null reference exception error everytime i run the program. how can i fix it? here's the code:

Protected Sub ContactNoGrid_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContactNoGrid.SelectedIndexChanged

 Me.txtContactNoID.Text = CType(ContactNoGrid.SelectedRow.FindControl("lblContactID"), Label).Text
Me.txtEditContact.Text = Convert.ToString(ContactNoGrid.SelectedRow.Cells(2).Text.Trim) 'CType(ContactNoGrid.SelectedRow.FindControl("lblContact"), Label).Text
Me.ddEditContactType.SelectedValue = Convert.ToString(ContactNoGrid.SelectedRow.Cells(1).Text.Trim)
Me.txtEditLocal.Text = IIf(Convert.ToString(ContactNoGrid.SelectedRow.Cells(3).Text.Trim) = " ", "", Convert.ToString(ContactNoGrid.SelectedRow.Cells(3).Text.Trim))
Me.btnDelContact.Visible = True  
 
''check if primary
Dim connStr As String = ConfigurationManager.ConnectionStrings("DBConn").ConnectionString
Dim sqlCon As New SqlConnection(connStr)
Dim sqlCmd As New SqlCommand("dbo.spCheckPrimaryContact", sqlCon)
Dim sqlDA As New SqlDataAdapter
Dim dt As New DataTable

sqlCmd.CommandType = Data.CommandType.StoredProcedure
sqlCmd.Parameters.AddWithValue("@iContactID", Me.txtContactNoID.Text)
sqlDA.SelectCommand = sqlCmd
sqlDA.Fill(dt)

If dt.Rows(0).Item("bPrimary") = True Then
Me.chkPrimaryContact.Checked = True
Else
Me.chkPrimaryContact.Checked = False
End If

ddlPrefix4.Enabled = True
ddlPrefix4.Visible = True
Dim sqlContactNo As New SqlCommand("dbo.spSeparateContactNumber", sqlCon)
Dim dt2 As New DataTable
sqlContactNo.CommandType = Data.CommandType.StoredProcedure
sqlContactNo.Parameters.AddWithValue("@iContactID", Me.txtContactNoID.Text)
sqlDA.SelectCommand = sqlContactNo
sqlDA.Fill(dt2)
Dim test As String = dt2.Rows(0).Item(0)
ddlPrefix4.Items.FindByText(test).Selected = True
txtEditContact.Text = dt2.Rows(0).Item(1)
End Sub





我在ddlPrefix4.Items.FindByText(test)。得到=真行



我尝试了什么:



我试过找到空但我没有一个



I get it at "ddlPrefix4.Items.FindByText(test).Selected = True" line

What I have tried:

I tried finding the null but i dont have one

推荐答案

无论如何,不​​要害怕使用VS调试器。在相关行旁边放置一个断点。应用程序中可能没有null,但FindByText(test)可能返回null(搜索值不在列表中)和.selected将其吹灭。



以下是Google关于如何使用调试器的视频列表



调试器视频 [ ^ ]



以下是有关如何使用VS调试器的文章的链接。

在Visual Basic .NET中进行调试 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]
Anyway, don't be afraid to use the VS debugger. Put a break point next to the line in question. There could be a likelihood there is no null in the application but FindByText(test) could be returning null (the search value not in the list) and .selected blow it out.

Here are list of videos from Google on how to utilize the debugger

Debugger Video[^]

Here are links to article on how to use the VS debugger.
Debugging in Visual Basic .NET[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


这篇关于空引用异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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