Furqan Sehgal
Furqan Sehgal

Reputation: 4997

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 object."

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")"

Upvotes: 0

Views: 210

Answers (2)

Nighil
Nighil

Reputation: 4129

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

Upvotes: 1

Grzegorz Gierlik
Grzegorz Gierlik

Reputation: 11232

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 for vrSQsNoChosen array and Rows collection?).
  • Item("QNo") value when error occurs (maybe is null?).

Also -- VB.NET implements += operator so you can write:

  w += 1

instead of:

 w = w + 1

Upvotes: 0

Related Questions