2015-12-02 22 views
-1

您好所有我有一個問題,當嘗試在我的課「響應」與VB.Net添加數據,我運行debbug得到一個錯誤消息「檢測空ReferenceException」你知道我應該糾正這個錯誤嗎?可以幫助我。謝謝。問題與VB錯誤消息「檢測NullReferenceExceptio」

下面部分的代碼

Module Module1 
Sub Main() 
    try 
    dim res as new response 

    a.customers(1).phoneNumber="11023321" 
    a.customers(2).phoneNumbre="11023300" 
    a.expand=False 
    a.include=false 


En sub 


Public Class Response 

    Public Property Customers As List(Of Customer) 
    Public Property Expand As Boolean 
    Public Property Include As Boolean 

End Class 

Public Class Customer 

    Public Property PhoneNumber As String 

End Class 
+0

是不完整的錯誤消息,並且沒有說在哪一行它發生 – Plutonix

回答

0

這可能是a.customers永遠不會被初始化,所以它是空(順便說一下,這應該是res.Customers,但我會認爲這是一個錯誤)。即使它不爲空,它仍然是空的,所以a.customers(1)會給你一個ArgumentOutOfRangeException。您可能需要從List(Of T)類的文檔開始,瞭解如何使用它。

像這樣的東西應該更接近你正在尋找:

Sub Main() 

    Dim res As New Response() 
    ' Populate customers with instances of the Customer class 
    res.Customers.Add(New Customer() With { .PhoneNumber = "11023321" }) 
    res.Customers.Add(New Customer() With { .PhoneNumber = "11023300" }) 
    res.Expand = False 
    res.Include = False 

End Sub 

Public Class Response 

    ' Initialize the Customers list 
    Public Property Customers As List(Of Customer) = New List(Of Customer)() 
    Public Property Expand As Boolean 
    Public Property Include As Boolean 

End Class 
+0

Wooow馬克......現在工作得很好。謝謝 – PJUAREZG