【问题标题】:Getting an ASP.NET error System.NullReferenceException: Object reference not set to an instance of an object?获取 ASP.NET 错误 System.NullReferenceException:对象引用未设置为对象的实例?
【发布时间】:2011-10-30 19:36:04
【问题描述】:

我对 ASP.NET 和 VB 以及 C# 完全陌生。我正在尝试将客户添加到数据库中的联系人列表中。然后可以引用列表来调用它们。

但是当我尝试运行它时,我得到 System.NullReferenceException: Object reference not set to an instance of an object。在第 19 行。

这是我的代码:

第 1 页是默认页面...它连接到数据库并获取联系人信息并允许我选择当前联系人并将它们添加到单独页面上的列表框中:

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

下一段代码允许我将联系人添加到列表框中:

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

错误发生在 default2 类的第二组代码中的第 19 行(For Each customerEntry As DictionaryEntry In Customer)。有任何想法吗?我对 ASP.NET 完全陌生,只是想学习。不幸的是,我更喜欢 PHP、Java 和 Actionscript。

【问题讨论】:

标签: asp.net vb.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

您在此处初始化 Customer,然后退出 Session("Cart")

我想你的意思是

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。

【讨论】:

  • 使代码运行的伟大捕获。现在我需要找出为什么它没有在列表框中显示任何客户名称。
  • 从略读代码我猜这是因为您实际上并没有将 Customer 对象存储回会话中,而只是检索它。您可能希望在执行重定向之前执行此操作。
  • 嗯,我需要多读一点。我不太确定你在指出什么。我以为这是在存储它(customerItem.Name = SelectedCustomer.ToString)我一定是误会了
  • 我的意思是您永远不会将更新的客户对象存储回会话中。在您的 AddToCustomer 函数结束时,我希望有这样一行 Session("Customer") = customer.否则,当您在调用 AddToCustomer 后直接执行重定向时,您对本地客户对象所做的更改将丢失
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
相关资源
最近更新 更多