如何修复错误“对象引用未设置为对象的实例”?

时间:2013-12-09 09:01:52

标签: asp.net nullreferenceexception

在这个程序(VB,ASP.NET 2010)中,我创建了三个字段:accnonamebalance,以及以下按钮:create,{{ 1}},destroyset。 但是,在点击getset方法时,它会出现以下异常:get

Default.aspx.vb

object reference not set to an instance of an object

Account.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

2 个答案:

答案 0 :(得分:0)

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

Dim 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

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

答案 1 :(得分:0)

使用下面的代码。你需要初始化页面加载方法,需要检查是回发属性。

 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