【发布时间】:2013-12-26 08:00:38
【问题描述】:
在这个程序(VB,ASP.NET 2010)中,我创建了三个字段:accno、name 和 balance,以及以下按钮:create、destroy、set 和 @987654328 @。
但是在单击set 或get 方法时,会出现以下异常: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