ASP .NET错误作为对象引用未设置为对象的实例 [英] ASP .NET Error as object reference not set to an instance of an object

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

问题描述





我收到错误对象引用没有设置为对象的实例,而我运行这段代码,让我知道是什么可以做到摆脱这个。





Hi,

I am getting the error "object reference not set to an instance of an object" while i run this piece of code, let me know what can be done to get rid of this.


string cntry = Session["ddlCountry"].ToString();

            if (cntry == "US")
            {
                lnkUrl.Text = string.Empty;
                if (lnkUrl == null ||  lnkUrl.Text.Length == 0)
                {
                    lnkUrl.Text = "http://www.google.com";
                }

推荐答案

这基本上是由于Session对象。

这是因为会话没有价值我猜。

所以在访问Session值之前做一件事就检查会话是否存在。如果存在则只将此会话值分配给变量。





示例:

将此代码放入page_Load method.First评论会话初始化,稍后检查取消评论并检查。希望你能得到你正在寻找的答案。





This is basically due to the Session object.
This is because the session is not having value i guess.
So do one thing before accessing the Session value check whether the session is exists or not. If exists then only assign this session value to variable.


Example:
Put this code in the page_Load method.First comment the Session initialization and check later on un comment and check this. Hope you will get the answer what you are searching.


Session["ddlCountry"] = "US";

if (Session["ddlCountry"]!=null)
{
    string cntry = Session["ddlCountry"].ToString();

    if (cntry == "US")
    {
        lnkUrl.Text = string.Empty;
        if (lnkUrl == null || lnkUrl.Text.Length == 0)
        {
            lnkUrl.Text = "http://www.google.com";
        }
    }
}
else
{
    lnkUrl.Text="Value not present in Session.";
}







好​​运。




Best of luck.


为什么使用对象的属性,然后将其检查为null?

Why are you using a property of an object, and then later checking it for null?
lnkUrl.Text = string.Empty;
 if (lnkUrl == null ||  lnkUrl.Text.Length == 0)

无论如何,第二个文本完全没用,因为前一行间接地将Length设置为零!

交换它结束:

The second text is completely useless anyway, because the previous line indirectly sets the Length to zero anyway!
Swap it over:

if (lnkUrl != null)
   {
   lnkurl.Text = "http://www.google.com";
   }

并且不要尝试设置空值的属性!

And don't try to set properties of null values!


在使用它之前检查Session [ddlCountry]是否有值。检查,如果你的lnkUrl设置为有效控制。

所以代码可能是这样的:

Check if you Session["ddlCountry"] has value before working with it. Check, if your lnkUrl is set to valid control.
So code might be like this:
if(Session["ddlCountry"] != null)
{
    var cntry = Session["ddlCountry"].ToString();
    if (cntry.Equals("US"))
    {
        //You cannot use this control, if lnkUrl is null
        //Since you're  setting lngUrl.Text = string.Empty, it will have 
        //Text.Length == 0  in all cases; so consider changing your logic. 
        //Probably like in code below.
        if(lnkUrl != null)
        {
            if(lnkUrl.Text.Length == 0)
            {
                lnkUrl.Text = "http://www.google.com";
            }
        }
    }
}


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

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