Null reference exception

Discussion in 'ASP .Net' started by Garg, Feb 15, 2007.

  1. Garg

    Garg Guest

    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??????
     
    Garg, Feb 15, 2007
    #1
    1. Advertising

  2. On Feb 15, 6:10 am, "Garg" <> wrote:
    > 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

    --
    http://ajaxwidgets.com
    Free ASP.NET Ajax Widgets NOW!
     
    Thomas Hansen, Feb 15, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George
    Replies:
    1
    Views:
    1,948
    George
    Jan 29, 2004
  2. =?Utf-8?B?TWF0dA==?=

    Null Reference Exception When Using Sesssion State

    =?Utf-8?B?TWF0dA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    10,828
    KrazeeEyezKilla
    Apr 26, 2010
  3. ¿À¼º¿µ
    Replies:
    1
    Views:
    590
    Steve C. Orr [MVP, MCSD]
    Feb 9, 2006
  4. Replies:
    5
    Views:
    27,530
    Mike Schilling
    Mar 29, 2006
  5. Ganesh Palaniappan

    Null Reference Exception

    Ganesh Palaniappan, Aug 2, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    591
    Ganesh Palaniappan
    Aug 3, 2006
Loading...

Share This Page