Null reference exception

  • Thread starter Thread starter Garg
  • Start date Start date
G

Garg

I have declared an array and when i use it in vb.net, I get a warning
that
variable arrClash has been used before it has been assignes a value. A
null reference exception could result at run time. This is my piece of
code;

Dim arrClash() As String

Dim i As Integer
Dim k As Integer
i = 0

For k = 1 To 4 Step 1
Dim drpDown As New DropDownList
drpDown = Page.FindControl("dropBasis" + k.ToString)
If drpDown.SelectedValue <> "" Then
arrClash(i) = drpDown.SelectedValue
i += 1
End If
Next

Please tell me how do I set object rerference to null??????
 
I have declared an array and when i use it in vb.net, I get a warning
that
variable arrClash has been used before it has been assignes a value. A
null reference exception could result at run time. This is my piece of
code;

Dim arrClash() As String

Dim i As Integer
Dim k As Integer
i = 0

For k = 1 To 4 Step 1
Dim drpDown As New DropDownList
drpDown = Page.FindControl("dropBasis" + k.ToString)
If drpDown.SelectedValue <> "" Then
arrClash(i) = drpDown.SelectedValue
i += 1
End If
Next

Please tell me how do I set object rerference to null??????



What you don't understand is that DECLARING and INSTANTIATING is two
different things in the .Net world...
You probably come from a dynamic language like JavaScript or something
where this isn't all that important...

What you have done in the above is to say that "I've got this variable
which I'll use somewhere" but you still haven't
actually CREATED that variable abd hence the warning...!!

To make a long story short use:
Dim x as New List(Of String)

....instead of your string array, and read a book on the .Net
framework...!!
And while you're at it, ditch VB.NET and learn a "real" programming
language... (hint; C#... ;)

..t
 
Back
Top