[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
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)
Re: Null Reference Exception Help.
I have assigned it a value:
Code:
strSkillData = Split(LCase(strSkillSet(i)), ",")
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.
Re: Null Reference Exception Help.
Quote:
Originally Posted by
stanav
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?
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.
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
Re: Null Reference Exception Help.
Thanks mate that worked. :)