对象引用未设置为对象的实例 [英] Object reference not set to instance of an object

查看:85
本文介绍了对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

i我正在尝试在gridview的RowDataBound事件中设置位于gridview中的标签文本。该代码的代码如下:





Hello,
i am trying to set label text which resides in gridview on RowDataBound event of gridview.The code for the same is as follows:


protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int day=Convert.ToInt32(DropDownList3.SelectedValue);
                int days = System.DateTime.DaysInMonth(2013,day);
                Label lab = (Label)e.Row.FindControl("d");
                lab.Text=days.ToString();
            }
        }



在上面的代码中,''''是gridview中标签的id。在突出显示的行中,我收到错误对象参考没有设置为对象的实例。据我所知,新的标签没有被创建。然后如何将在此事件中计算的值分配给gridviews标签?任何帮助都非常适合...

问候


In the above code,''d'' is the id of the label in the gridview.On the highlighted line i am getting error that OBJECT REFERENCE NOT SET TO INSTANCE OF AN OBJECT.As per as i understand,the new label is not being created.then how to assign values calculated in this event to gridviews label?Any help is greatly appriciated...
Regards

推荐答案

这段代码不会创建标签,它只是试图通过 id 找到它,你可能已经明白了。搜索失败。您需要在使用任何实例成员解除引用之前检查结果为null,在本例中为 Text 。您的代码没有显示为什么您没有找到您期望的标签。



最有可能的是,这是ASP.NET(总是标记您在问题中使用的应用程序类型或UI库,当涉及到UI时)。做一件简单的事。使这段代码可以运行(注释掉可疑部分),从客户端查看页面并从浏览器中查看纯HTML页面(查看页面源或类似内容)。查看源代码,找出 id 或您生成的其他内容有什么问题。



-SA
This code does not create a label, it just tries to find it by id, as you probably understand. The search fails. You need to check the result for null before dereferencing it by using any of its instance member, in this case, Text. Your code does not show why you don''t find your label where you expect it.

Most likely, this is ASP.NET (always tag your application type or UI library you use in your question, when it comes to UI). Do a simple thing. Make this code runnable (comment out questionable parts), look at the page from the client side and look at your pure HTML page from the browser (View Page Source or something like that). Look at the source and find out what''s wrong with the id or whatever else you have generated.

—SA


对象引用未设置为对象的实例



当您尝试使用属性或调用null对象的方法时,会发生此错误。更多细节:此处 [ ^ ]



Visual Studio DEBUGGER的简单使用可以告诉你对象因为它正在发生。只需查看堆栈跟踪并将调试器放在该行上即可。检查该行的对象,看看是否有任何一个为null,并且您正在尝试使用该对象属性。处理相同。





根据您上面分享的内容,最有可能 lab 是NULL。确保行中有一个名为d的控件。

此外,可能是这个错误是针对网格标题,其中没有标签''d''控制。

如果是这样,请尝试:

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.


Based on what you have shared above, most probably lab is NULL. Make sure there is a control named ''d'' in the row.
Also, it may be this error is for grid header where no label ''d'' control is present.
If so, try:
DataControlRowType rtype = e.Row.RowType;  
if (rtype == DataControlRowType.DataRow && rtype != DataControlRowType.Footer && rtype != DataControlRowType.Header)  
{
 //
} 



在DEBUG模式下方法中简单使用断点将清除所有疑点和可能的修复。


A simple use of breakpoint in the method in DEBUG mode will clear all the doubts and possible fix.


这篇关于对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆