Results 1 to 8 of 8

Thread: [RESOLVED] Null Reference Exception Help.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    127

    Resolved [RESOLVED] Null Reference Exception Help.

    Hey i keep getting this null reference exception error and i can't figure out what i need to do to fix it.

    I have this in the form
    Code:
            Private Sub cbLookUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbLookUp.Click
            Dim webDownload As New Net.WebClient
            Dim strHtml As String
            Dim strSkillSet() As String
            Dim strSkillData() As String
            Dim i As Integer
            'Dim a As Integer
            Dim lol As Control() = clsArray.getMixedControlArray(Me, "lblHS")
            strHtml = webDownload.DownloadString(strHighscores & txtUsername.Text)
            strSkillSet = Split(LCase(strHtml), " ")
            For i = 0 To UBound(strSkillSet)
                strSkillData = Split(LCase(strSkillSet(i)), ",")
                'For a = 0 To UBound(strSkillData)
                    strHSData(i).intRank = strSkillData(0)
                    strHSData(i).intLevel = strSkillData(1)
                    strHSData(i).intXp = strSkillData(2)
                'Next
                lol(i).Text = strHSData(i).intLevel
            Next
        End Sub
    and this in a module:
    Code:
        Public strHSData() As HighScores
    
        Public Structure HighScores
            Public intRank
            Public intLevel
            Public intXp
        End Structure
    Throw's the error on the first loop.
    Thanks
    Last edited by FredFrothen; Feb 4th, 2011 at 01:37 PM.
    EXPERIENCED VB6 CODER
    IF YOU SEE ANY FAILURE IN MY CODING, PLEASE LET ME KNOW AS I'VE ONLY JUST STARTED LEARNING VB.NET AND I WILL APPRECIATE ANY HINTS AND TIPS YOU HAVE TO OFFER!

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Null Reference Exception Help.

    You declare the string array strSkillData but did not assign any value to it, therefore it is nothing.
    Later, you try to access it as if it already contains data, and thus you get the null reference exception.
    Code:
    Dim strSkillData() As String '< at this point, strSkillData is Nothing
    
    'Then you try to access element 0 of the array, since strSkillData is nothing, you get the exception.
    strHSData(i).intRank = strSkillData(0)
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    127

    Re: Null Reference Exception Help.

    I have assigned it a value:
    Code:
    strSkillData = Split(LCase(strSkillSet(i)), ",")
    EXPERIENCED VB6 CODER
    IF YOU SEE ANY FAILURE IN MY CODING, PLEASE LET ME KNOW AS I'VE ONLY JUST STARTED LEARNING VB.NET AND I WILL APPRECIATE ANY HINTS AND TIPS YOU HAVE TO OFFER!

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Null Reference Exception Help.

    Then the strHSData array is null. It's the same idea. You declared the array without giving it a dimension or assigning a value to it.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    127

    Re: Null Reference Exception Help.

    Quote Originally Posted by stanav View Post
    Then the strHSData array is null. It's the same idea. You declared the array without giving it a dimension or assigning a value to it.
    hmm ok, then how do i go about assigning it a value? Because i thought:
    Code:
    strHSData(i).intRank = strSkillData(0)
                    strHSData(i).intLevel = strSkillData(1)
                    strHSData(i).intXp = strSkillData(2)
    Would do that, is it different because i've made it a structure?
    EXPERIENCED VB6 CODER
    IF YOU SEE ANY FAILURE IN MY CODING, PLEASE LET ME KNOW AS I'VE ONLY JUST STARTED LEARNING VB.NET AND I WILL APPRECIATE ANY HINTS AND TIPS YOU HAVE TO OFFER!

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Null Reference Exception Help.

    You declared strHSData as an array. Doing that creates an array with no size. You would have to dimension it to the right size to hold anything. If you don't know the size that will be needed when you start out, or if that size is changing, you should be using a List (of T) rather than an array, but even then you will have to create the list before using it.
    My usual boring signature: Nothing

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Null Reference Exception Help.

    Since you don't know the size of the array when declaring it, I suggest you to use a List(Of T) as Shaggy already said.
    Code:
    Dim strHDData As New List(Of HighScores)
    
    'Then in you loop, you do:
    Dim scores As HighScores
     For i = 0 To UBound(strSkillSet)
                strSkillData = Split(LCase(strSkillSet(i)), ",")
                'For a = 0 To UBound(strSkillData)
                   'Create a new HighScores
                    scores = New HighScores()
                    'Set its values
                    scores.intRank = strSkillData(0)
                    scores.intLevel = strSkillData(1)
                    scores.intXp = strSkillData(2)
                    'Then add it to the list
                    strHDData.Add(scores)
                'Next
                lol(i).Text = strHSData(i).intLevel
            Next
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    127

    Re: Null Reference Exception Help.

    Thanks mate that worked.
    EXPERIENCED VB6 CODER
    IF YOU SEE ANY FAILURE IN MY CODING, PLEASE LET ME KNOW AS I'VE ONLY JUST STARTED LEARNING VB.NET AND I WILL APPRECIATE ANY HINTS AND TIPS YOU HAVE TO OFFER!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width