简体   繁体   English

System.NullReferenceException:对象引用未设置为对象的实例

[英]System.NullReferenceException: Object reference not set to an instance of an object

I am using asp.net LoginView to show different data to authenticated and anonymous users. 我正在使用asp.net LoginView向经过身份验证的用户和匿名用户显示不同的数据。

<asp:LoginView ID="LoginView1" Runat="server">
    <LoggedInTemplate>
        <asp:Label ID="Foo" runat="server" />
    </LoggedInTemplate>
    <AnonymousTemplate>
        <asp:Label ID="Bar" runat="server" />
    </AnonymousTemplate>
</asp:LoginView>

I then access these labels in my c# file like this: 然后,我像这样在c#文件中访问这些标签:

Label Foo = (Label)LoginView1.FindControl("Foo");
Foo.Text = "whatever";

The error I am getting reads: 我得到的错误是:

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

Well presumably at execution time, the user isn't logged in - so there's no control with an ID of Foo , so FindControl is returning null . 大概是在执行时,用户没有登录-因此没有ID为Foo控件,因此FindControl返回null You should either detect whether the user is logged in or not separately and ask for the right control, or check whether Foo is null before you use it. 您应该检测用户是否单独登录并请求正确的控件,或者在使用Foo之前检查Foo是否为null (You might want to consider renaming your local variable to foo to be more in tune with C# conventions, too.) (您可能还想考虑将本地变量重命名为foo ,以便更符合C#约定。)

Try this: 尝试这个:

Label Foo = (Label)LoginView1.FindControl("Foo");
if(Foo != null)
{
    Foo.Text = "whatever";
}

Now you will not get the error, but if Foo is null, then your label's text will not update. 现在您将不会收到错误,但是如果Foo为null,则标签的文本将不会更新。 You need to determine why it cannot find the Label named "Foo". 您需要确定为什么找不到名为“ Foo”的Label

You need to check for nulls: 您需要检查是否为空:

object labelObj = LoginView1.FindControl("Foo")
if(labelObj != null)
{
    Label Foo = (Label)labelObj;
    if(Foo.Text!=null)
        Foo.Text="whatever";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.NullReferenceException - 未将对象引用设置为对象的实例 - System.NullReferenceException – Object reference not set to an instance of an object Foreach System.NullReferenceException:未将对象引用设置为对象的实例 - Foreach System.NullReferenceException: Object reference not set to an instance of an object 类型&#39;System.NullReferenceException&#39;的异常:对象引用未设置为对象的实例 - An exception of type 'System.NullReferenceException' : Object reference not set to an instance of an object 错误:System.NullReferenceException:对象引用未设置为对象的实例 - Error :System.NullReferenceException: Object reference not set to an instance of an object Xamarin android System.NullReferenceException:未将对象引用设置为对象的实例 - Xamarin android System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:未将对象引用设置为对象的实例 - System.NullReferenceException: Object reference not set to an instance of an object 如何修复 System.NullReferenceException:Object 引用未设置为 object 的实例 - How to fix System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:对象引用未设置为对象的实例 - System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:“对象引用未设置为 object 的实例。” 问题 - System.NullReferenceException: „Object reference not set to an instance of an object.” problem 会话{“对象引用未设置为对象的实例。”} System.Exception {System.NullReferenceException} - session {“Object reference not set to an instance of an object.”} System.Exception {System.NullReferenceException}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM