你调用的对象是空的 [英] Object reference not set to an instance of an object
问题描述
''正如Grif告诉我的那样,我修改了我的代码,我从加载事件中删除了代码块,并在row enter事件中调用了auto函数,仍然存在问题.
``我已经声明了一个变量索引来保存gridview的当前行索引
''I have a modified my code as Grif told me, i removed code block from load event and I call a function auto at the row enter event,Still problem Persists.
''I have declared a variable index to hold the current row index of the gridview
public Class RecuritmentManagement
Dim con As New SqlConnection("Integrated Security=SSPI;Initial catalog=Equation;server=localhost\SQLEXPRESS")
public index as Integer
Private Sub RecruitmentManagement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sCommand As SqlCommand
Dim sAdapter As SqlDataAdapter
Dim sBuilder As SqlCommandBuilder
Dim sDs As DataSet
Dim sTable As DataTable
Try
Dim sql As String = "select Id,SrNo,Name,DOB,ContactNumber,ResNumber, Location,Qualification,CurrentOrganizatn,WorkExp,CalledBy,EQId,Date,SentTo,HRStatus,Process,Department,Designation from BasicInfo,CompanyInfo where BasicInfo.CId=CompanyInfo.CIds"
sCommand = New SqlCommand(sql, con)
sAdapter = New SqlDataAdapter(sCommand)
sDs = New DataSet()
sAdapter.Fill(sDs)
sTable = sDs.Tables(0)
BasicDetailGrid.DataSource = sDs.Tables(0)
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Private Sub BasicDetailGrid_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles BasicDetailGrid.RowEnter
auto()
End Sub
Public Sub auto()
Try
Dim cmd1 As New SqlCommand
Dim f2
con.Open()
Dim cmd2 As New SqlCommand("select COUNT(*) from BasicInfo", con)
f2 = cmd2.ExecuteScalar
con.Close()
index = BasicDetailGrid.CurrentRow.Index ''******* ERROR HERE *******
If (CInt(f2) = 0) Then
Me.BasicDetailGrid.Rows(index).Cells(0).Value = "1";
Else
...
End If
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
End Class
''先谢谢了.
[edit]已添加代码块-OriginalGriff [/edit]
''Thanks In Advance.
[edit]Code block added - OriginalGriff[/edit]
推荐答案
是.是吗?
Yes. So?
BasicDetailGrid.DataSource = sDs.Tables(0)
index = BasicDetailGrid.CurrentRow.Index
如果这两行中的第二行出现对象未设置为实例"错误,我不会感到惊讶.没有当前行.并且直到用户选择了一个.既然您还没有给他/她看过,那还会有一段时间...
我建议无论您在何处使用index
,都最好将其加载到此处(以便在用户输入任何信息后都能获得最新版本),并在使用前检查其有效性(以防用户未使用它).选择了一行...
If you are getting a "object not set to an instance" error on the second of these two lines I am not surprised. There is no current row. And won''t be until the user has selected one. Which, since you haven''t shown it to him / her yet, will be a while...
I would suggest that wherever you use index
it would be better to both load it there (so that you get the latest version after any user input) and check it for validity before you use it, in case the user hasn''t selected a row yet...
添加到其他答案:
"System.NullReferenceException:对象引用未设置为对象的实例."
这仅表示您正在尝试访问null的对象的属性.
使用Visual Studio DEBUGGER将告诉您确切的行和为null的对象.找到它并进行处理.
Adding to other answer:
" System.NullReferenceException: Object reference not set to an instance of an object."
This simply means that you are trying to access a property of an object that is null.
Using Visual Studio DEBUGGER will tell you exact line and the object that is null. Find it and handle the same.
这篇关于你调用的对象是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!