Null reference exception

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??????
 
T

Thomas Hansen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top