【问题标题】:How to fix the error "Object reference not set to an instance of an object"?如何修复错误“对象引用未设置为对象的实例”?
【发布时间】:2013-12-26 08:00:38
【问题描述】:

在这个程序(VB,ASP.NET 2010)中,我创建了三个字段:accnonamebalance,以及以下按钮:createdestroyset 和 @987654328 @。 但是在单击setget 方法时,会出现以下异常:object reference not set to an instance of an object

Default.aspx.vb

Partial Class _Default
    Inherits System.Web.UI.Page

    Dim obj As account 'declaring the obj of class account

    Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click
        obj = New account 'initializing the object obj on class accounts
    End Sub    

    Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
        'sending the values from textboxes to accounts class through method setdata
        Try
            obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
        'calling the method getdata to view the output
        Try
            obj.getdata()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Protected Sub btn_destroy_Click(sender As Object, e As System.EventArgs) Handles btn_destroy.Click
        'calling the constructor
        obj = Nothing
    End Sub
End Class

Account.vb

Imports Microsoft.VisualBasic

Public Class account

    Private accno As Integer
    Private acc_name As String
    Private bal As Integer

    'constructor
    Public Sub New()
        MsgBox("object created")
    End Sub

    'public method to populate above three private variable

    Public Sub setdata(ByVal a As Integer, ByVal b As String, ByVal c As Integer)
        Me.accno = a
        Me.acc_name = b
        Me.bal = c
    End Sub

    Public Sub getdata()
        MsgBox(Me.accno.ToString + vbNewLine + Me.acc_name + vbNewLine + Me.bal.ToString)
    End Sub

    'destructor
    Protected Overrides Sub finalize()
        MsgBox("object destroyed")
    End Sub

End Class

【问题讨论】:

  • 你为什么大喊大叫?你的键盘似乎没问题。
  • 我希望所有这些MsgBoxs 仅用于临时调试-您知道它们仅在使用开发服务器运行时才有效,即使它们确实有效,然后在服务器上运行,不是(必须)显示网页的同一台机器。
  • NullReferenceException 的几乎所有情况都是相同的。请参阅“What is a NullReferenceException in .NET?”获取一些提示。
  • 我修正了标题、单词、大小写和格式,以使问题更具可读性

标签: asp.net nullreferenceexception


【解决方案1】:

您已使用 New 关键字初始化对象...

将 obj 设为新帐户

Default.aspx.vb Partial Class _Default Inherits System.Web.UI.Page

'declaring the obj of class account

Dim obj As NEW account    


Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click

    'initializing the object obj on class accounts
    obj = New account



End Sub


Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click

    'sending the values from textboxes to accounts class through method setdata

    Try
        obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))

    Catch ex As Exception

        MsgBox(ex.Message)
    End Try


End Sub

Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click

    'calling the method getdata to view the output

    Try

        obj.getdata()

    Catch ex As Exception

        MsgBox(ex.Message)
    End Try

End Sub

Protected Sub btn_destroy_Click(sender As Object, e As System.EventArgs) Handles btn_destroy.Click

    'calling the constructor

    obj = Nothing

End Sub
End Class

如果有效,请将其标记为答案。

【讨论】:

  • 在使用这个之后......当我点击 msgbox 中的 GET 按钮时,它给出的值是 0 0
  • 这是因为您在 Account Class 中的功能。请检查这个 Public Sub getdata() MsgBox(Me.accno.ToString + vbNewLine + Me.acc_name + vbNewLine + Me.bal.ToString) End Sub
  • 那么我应该怎么做...对于set和get方法...?
【解决方案2】:

使用下面的代码。您需要在页面加载方法中进行初始化,并且需要检查回发属性。

 Default.aspx.vb Partial Class _Default Inherits System.Web.UI.Page

        'declaring the obj of class account

        Dim obj As account 

        Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
            if not ispostback then
                 obj = New account
            end
       End Sub

        Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click

            'initializing the object obj on class accounts
            obj = New account



        End Sub


        Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click

            'sending the values from textboxes to accounts class through method setdata

            Try
                obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))

            Catch ex As Exception

                MsgBox(ex.Message)
            End Try


        End Sub

        Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click

            'calling the method getdata to view the output

            Try

                obj.getdata()

            Catch ex As Exception

                MsgBox(ex.Message)
            End Try

        End Sub

        Protected Sub btn_destroy_Click(sender As Object, e As System.EventArgs) Handles btn_destroy.Click

            'calling the constructor

            obj = Nothing

        End Sub
        End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 2011-07-09
    • 2012-08-31
    • 2011-11-21
    • 2012-10-01
    相关资源
    最近更新 更多