对象引用未设置为对象的实例 - 错误

时间:2017-06-23 14:50:45

标签: arrays vb.net list nullreferenceexception

我有一个枚举,它是数组列表(可选,你可以添加最多5次的项目)

Public DiseaseTreatment() As TreatmentsetDiseaseTreatment

Public Enum TreatmentsetDiseaseTreatment
'''<remarks/>
<System.Xml.Serialization.XmlEnumAttribute("01")>  _
Item01

''<remarks/>
Antibiotics
''<remarks/>
<System.Xml.Serialization.XmlEnumAttribute("02")>  _
Item02

'''<remarks/>
<System.Xml.Serialization.XmlEnumAttribute("Pain Killers)")>  _
PainKillers

Dim temptreatment As List(Of TreatmentsetDiseaseTreatment)= New List(Of TreatmentsetDiseaseTreatment)()
temptreatment.Add("Painkiller")
For i As Integer = 0 To temptreatment(i) - 1
   test.TreatmentTypes(i) = temptreatment(i)
Next

当我将鼠标悬停在tempTreatment(i)上时,它有正确的字符串"painkiller",所以我不明白它是如何投掷NullReferenceException的?

1 个答案:

答案 0 :(得分:4)

您希望在数组末尾停止,而不是在数组中x项的末尾停止。

For i As Integer = 0 To temptreatment.Count - 1

For i As Integer = 0 To temptreatment(i) - 1