Problem storing value in array
I want to store value in array, from my database. I am using following code but it return error: "Object reference not set to an instance of an objec开发者_运维百科t."
Code is:
Dim w as integer=0
Do While DsChooseSQsNow.tblChooseSQs.Rows.Count > w
vrSQsNoChosen(w) = DsChooseSQsNow.tblChooseSQs.Rows(w).Item("QNo")
vrTotalSQsChosen = vrTotalSQsChosen + 1
w = w + 1
Loop
Error comes on "vrSQsNoChosen(w) = DsChooseSQsNow.tblChooseSQs.Rows(w).Item("QNo")"
try to print the value DsChooseSQsNow.tblChooseSQs.Rows(w).Item("QNo")
or debug the code
"Object reference not set to an instance of an object." means Item("QNo") may be null
There are many reasons why this code fails. I would start checking from:
- Size (and boundings) of
vrSQsNoChosen
array. w
value when error occurs (is valid index forvrSQsNoChosen
array andRows
collection?).Item("QNo")
value when error occurs (maybe isnull
?).
Also -- VB.NET implements +=
operator so you can write:
w += 1
instead of:
w = w + 1
精彩评论