2011-10-30 17 views
0

私はASP.NETとVBだけでなくC#も全く新しいです。私は顧客をDBから連絡先リストに追加しようとしています。リストを参照して呼び出すことができます。ASP.NETエラーを取得するSystem.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていませんか?

しかし、私はそれを実行しようとすると、私はSystem.NullReferenceExceptionを取得します:オブジェクト参照は、オブジェクトのインスタンスに設定されていません。ライン19

ではここに私のコードです:

1ページがデフォルトページです...それは、データベースに接続し、連絡先情報をつかみ、私は現在の連絡先を選択し、上のリストボックスに追加することができます別のページ:

Partial Class Default2 
    Inherits System.Web.UI.Page 
    Private Customer As SortedList 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     Customer = Me.GetCustomer 
     If Not IsPostBack Then Me.DisplayCustomer() 

    End Sub 
    Private Function GetCustomer() As SortedList 
     If Session("Customer") Is Nothing Then 
      Session.Add("Customer", New SortedList) 
     End If 
     Return CType(Session("Cart"), SortedList) 
    End Function 
    Private Sub DisplayCustomer() 
     lstCustomer.Items.Clear() 
     Dim customerItem As Customer 
     For Each customerEntry As DictionaryEntry In Customer 
      customerItem = CType(customerEntry.Value, Customer) 
      lstCustomer.Items.Add(customerItem.Name) 
     Next 
    End Sub 

    Protected Sub lstCustomer_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstCustomer.SelectedIndexChanged 

    End Sub 
End Class 

エラーが各customerEnについて(19行目で発生します。コードの次のビットは、私は、リストボックスに連絡先を追加することができます

Imports System.Data 

Partial Class _Default 
    Inherits System.Web.UI.Page 

    Private SelectedCustomer As Customer 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ 
      Handles Me.Load 
     If Not IsPostBack Then 
      ddlCustomers.DataBind() 
     End If 
     SelectedCustomer = Me.GetSelectedCustomer 
     Me.DisplayCustomer() 
    End Sub 

    Private Function GetSelectedCustomer() As Customer 
     Dim dvTable As dataview = CType(AccessDataSource1.Select(_ 
      DataSourceSelectArguments.Empty), dataview) 
     dvTable.RowFilter = "CustomerID = " & ddlCustomers.SelectedValue 
     Dim drvRow As DataRowView = dvTable(0) 

     Dim customer As New Customer 
     customer.CustomerID = CInt(drvRow("CustomerID")) 
     customer.Name = drvRow("Name").ToString 
     customer.Address = drvRow("Address").ToString 
     customer.City = drvRow("City").ToString 
     customer.State = drvRow("State").ToString 
     customer.ZipCode = drvRow("ZipCode").ToString 
     customer.Phone = drvRow("Phone").ToString 
     customer.Email = drvRow("Email").ToString 
     Return customer 
    End Function 

    Private Sub DisplayCustomer() 
     lblAddress.Text = SelectedCustomer.Address 
     lblCityStateZip.Text = SelectedCustomer.City & ", " _ 
          & SelectedCustomer.State & " " _ 
          & SelectedCustomer.ZipCode 
     lblPhone.Text = SelectedCustomer.Phone 
     lblEmail.Text = SelectedCustomer.Email 
    End Sub 


    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click 
     If Page.IsValid Then 
      Dim customerItem As New Customer 
      customerItem.Name = SelectedCustomer.ToString 
      Me.AddToCustomer(customerItem) 
      Response.Redirect("CustomerList.aspx") 
     End If 
    End Sub 

    Private Sub AddToCustomer(ByVal customerItem As Customer) 
     Dim customer As SortedList = Me.GetCustomer 
     Dim customerID As String = SelectedCustomer.CustomerID 
     If customer.ContainsKey(customerID) Then 
      customerItem = CType(customer(customerID), Customer) 
     Else 
      customer.Add(customerID, customerItem) 
     End If 
    End Sub 

    Private Function GetCustomer() As SortedList 
     If Session("Customer") Is Nothing Then 
      Session.Add("Customer", New SortedList) 
     End If 
     Return CType(Session("Customer"), SortedList) 
    End Function 

End Class 

Default2クラスのコードの2番目のセットでCustomerとしてDictionaryEntryとして試してください)。何か案は?私はちょうど学ぶことを試みるASP.NETに全く新しいです。残念なことにPHP、Java、Actionscriptの方が家庭にいます。

+0

[.NETのNullReferenceExceptionは何ですか?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) –

答えて

1

私は

Private Function GetCustomer() As SortedList 
    If Session("Customer") Is Nothing Then 
     Session.Add("Customer", New SortedList) 
    End If 
    Return CType(Session("Cart"), SortedList) 
End Function 

はあなたがここにお客様を初期化するが、その後のセッション(「カート」)

からの撤退問題は、この関数の中でだと思う私は何をするものでは

Private Function GetCustomer() As SortedList 
    If Session("Customer") Is Nothing Then 
     Session.Add("Customer", New SortedList) 
    End If 
    Return CType(Session("Customer"), SortedList) 
End Function 
だと思います

顧客は常に初期化する必要がありますが、そうしないと、取得しているNullReferenceExceptionが発生する可能性があります。

+0

コードが実行される大きな罠です。今では、リストボックスで顧客名を表示していない理由を追跡する必要があります。 – allencoded

+0

Customerオブジェクトを実際にセッションに戻さずに取り出しただけなので、コードをスキミングしているからです。リダイレクトを行う前に、これを行うことをお勧めします。 – whudson05

+0

hummmもう少し読んでみる必要があります。私は、あなたが何を指摘しているのかよく分かりません。私はこれを保存していると思った(customerItem.Name = SelectedCustomer.ToString)。間違っているはずです。 – allencoded

関連する問題