NullReference异常错误-未将对象引用设置为对象的实例 [英] NullReference Exception error - Object Reference not set to an instance of an object
问题描述
谁能帮我解决这段代码,我需要尽快解决.编译
hi there,
Can anyone help me out with this code I need a solution asap. I keep getting Object reference not set error when i compile it
txt1.Text = ds.Tables("Email").Rows(0).Item("Subject")
似乎出错时,我一直在获取对象引用未设置错误!这是我的代码:
is where it seems to error! here is my code:
Partial Public Class MaintainWEDAutoEmail
Inherits System.Web.UI.Page
Dim csccode As New CommonCode
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If User.Identity.IsAuthenticated Then
If Session("Full_User_Name") = "" Then
Session("User_Name") = User.Identity.Name
Session("Full_User_Name") = csccode.FindUserFullName(Session("User_Name"))
End If
If Not Page.IsPostBack Then
If User.IsInRole("maintain-wed-email-update") Or User.IsInRole("superuser") Then
btnMaintainUpdate.Visible = True
Else
btnMaintainUpdate.Visible = False
End If
Dim ds As DataSet = New DataSet
ds = csccode.GetWEDAutoEmail
txt1.Text = ds.Tables("Email").Rows(0).Item("Subject")
txt2.Text = ds.Tables("Email").Rows(0).Item("Body")
txt3.Text = ds.Tables("Email").Rows(0).Item("Contact")
End If
Else
Response.Redirect("Login.aspx")
End If
End Sub
Protected Sub btnMaintainUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMaintainUpdate.Click
lblError.Text = ""
Try
lblError.Text = csccode.WEDAutoEmailUpdate(txt1.Text, txt2.Text, txt3.Text)
If lblError.Text = "" Then
lblError.Text = "WED Auto Email updated succesfully"
csccode.Log(Session("Full_User_Name"), "WED Auto Email Update", "Email Update")
Else
lblError.Text = "** Problem updating WED Auto Email " & lblError.Text & " **"
End If
Catch ex As Exception
lblError.Text = "** Problem updating WED Auto Email" & ex.Message & " **"
End Try
End Sub
End Class
在此先感谢您!
Thanks in advance!
推荐答案
您的代码:
Your code:
ds = csccode.GetWEDAutoEmail
txt1.Text = ds.Tables("Email").Rows(0).Item("Subject")
该错误意味着某处不存在.问题可能是以下情况之一:
1. GetWEDAutoEmail返回了什么都没有的数据集
2.数据集没有名为"Email"的表
3.表电子邮件"没有任何行
4.表电子邮件"没有名为主题"的列
您必须进行调试才能弄清楚哪个.在设置txt1中文本的行上放置一个断点.当达到断点时,使用快速监视来缩小丢失的部分的范围.您将需要修复GetWEAutoEmail或需要进行检查,然后再设置txt1.
***更新***
如何使用QuickWatch ...嗯, ^ ]上有一些有关该主题的文章...但是浏览它们,他们并没有很多漂亮的屏幕截图.但是也许您还是想看看它们.基本上,当您使程序运行并在断点处停止时,您可以突出显示变量并查看其值.因此,仅突出显示代码行中的ds
.此时,您可能会看到一个弹出窗口,其中显示了突出显示的更多信息和数据.您可以使用该弹出窗口,也可以右键单击并从菜单中选择QuickWatch.这会弹出一个菜单,您可以在其中键入变量和代码位,并查看它们的值.由于您已突出显示ds,因此ds应该显示为您要评估的表达式.如果它具有值,则可以在快速监视"屏幕上的网格中查看其所有属性和值.如果您看到ds
具有值,请在表达式"文本框中键入ds.Tables("Email")
,然后按Enter.现在,您正在检查THAT是否具有值.然后键入ds.Tables("Email").Rows(0)
以检查值.然后粘贴.Item("Subject")并检查.这有点难以解释,因此您必须花一点时间才能真正掌握它.希望这会有所帮助.
The error means that something isn''t there. The problem is likely one of these scenarios:
1. The GetWEDAutoEmail returned a dataset that was nothing
2. The dataset does not have a table named "Email"
3. The table "Email" does not have any rows
4. The table "Email" does not have a column named "Subject"
You will have to debug in order to figure out which. Place a break point on the line setting the text in txt1. When the breakpoint is hit, use quickwatch to narrow down which part is missing. You will either need to fix the GetWEAutoEmail or you will need to do checks before setting txt1.
*** UPDATE ***
How to use QuickWatch...well, google[^] has some articles on the topic...but glancing at them they don''t have many nice screenshots. But maybe you''ll want to look at them anyway. Basically when you''ve got your programming running and stopped at a breakpoint, you can highlight variables and see what their values are. So highlight just the ds
in your line of code. At this point, you can probably see a popup that shows you more information and data that you''ve highlighted. You can use that popup, or you can right click and select QuickWatch from the menu. This pops up a menu where you can type in variables and bits of code and see what their values are. Since you had ds highlighted, ds should appear as the Expression that you are evaluating. If it has a value, you''ll be able to see all of it''s properties and thier values in the grid on the QuickWatch screen. If you can see that the ds
has a value, then type in ds.Tables("Email")
in the Expression textbox and press enter. Now you are checking to see if THAT has a value. Then type in ds.Tables("Email").Rows(0)
to check for a value. Then tack on the .Item("Subject") and check that. It''s kind of hard to explain, so you''ll have to play with it a little bit to really get the hang of it. Hope this helps.
这篇关于NullReference异常错误-未将对象引用设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!