简体   繁体   English

未将对象引用设置为使用ASP.NET Project的对象的实例

[英]Object reference not set to an instance of an object working with ASP.NET Project

I'm having an issue comparing 2 objects in an ASP.NET Project, I'm not sure if I am a beginner or what but this is my actual code: 我在比较ASP.NET项目中的2个对象时遇到问题,我不确定我是初学者还是什么,但这是我的实际代码:

private void CheckFriendshipStatus()
    {


        if (Object.ReferenceEquals(Session["UserId"].ToString(), Session["CurrentProfileId"].ToString()))
        {
            btnAddAsFriend.Visible = false;
        }
        else
        {
            DataTable dt1 = new DataTable();
            string chkfriendRequest = "SELECT * FROM Friends WHERE (MyId='" + Session["UserId"].ToString() + "' and FriendId='" + Session["CurrentProfileId"].ToString() + "') OR (MyId='" + Session["CurrentProfileId"].ToString() + "' and FriendId='" + Session["UserId"].ToString() + "')";

Then I tried: 然后我尝试了:

    var obj1 = Session["UserId"].ToString();
    var obj2 = Session["CurrentProfileId"].ToString();


        if (obj1 == obj2)
        {
            btnAddAsFriend.Visible = false;
        }
    else
    {

and then I tried setting the value to null first and then assigning the session values. 然后我尝试先将值设置为null,然后分配会话值。 I'm out of ideas, any other one will be highly appreciated 我没有主意,将不胜感激

If you put strings into Session, you can get them back out as strings by casting. 如果将字符串放入Session中,则可以通过强制转换将它们作为字符串撤回。

var obj1 = (string)Session["UserId"]

If obj1 happens to be null, calling obj1.ToString() will cause a null reference exception. 如果obj1恰好为null,则调用obj1.ToString()将导致null引用异常。 Casting as shown above will avoid that exception (though it may still be a logical error if one of them is null, depending on the conventions in your program). 如上所示的转换将避免该异常(尽管如果其中之一为null,则仍然可能是逻辑错误,具体取决于程序中的约定)。

If they are strings, you want to use the == operator. 如果它们是字符串, ==使用==运算符。 Object.ReferenceEquals() returns true if the two variables refer to the exact same object. 如果两个变量引用完全相同的对象,则Object.ReferenceEquals()返回true。 Due to string interning, this would "coincidentally" commonly give you the expected result anyhow, but it is possible to turn off string interning. 由于字符串插入,无论如何这通常会“巧合”地为您提供预期的结果,但是可以关闭字符串插入。

UPDATE: 更新:

Based on your clarifying comment, it's almost certain that one or both of the Session variables you are calling .ToString() on is null. 根据您的澄清意见,几乎可以确定您正在调用.ToString()一个或两个Session变量为null。 Use the casting approach that I outline, and use a debugger to see which is null. 使用我概述的转换方法,并使用调试器查看哪个为空。

Either Session["UserId"] or Session["CurrentProfileId"] or btnAddAsFriend is null. Session["UserId"]Session["CurrentProfileId"]btnAddAsFriend为null。 Can't tell which from the details provided. 从提供的详细信息中无法分辨出哪个。

This Actually worked guys, 这实际上工作的家伙,

    var obj1 = Session["UserId"].ToString();
    var obj2 = Session["CurrentProfileId"].ToString();


    if (obj1 == obj2)
    {
       btnAddAsFriend.Visible = false;
    }

If this is happening to some of you that might be having the same code as step 1, this will resolve it! 如果对某些可能与步骤1相同的代码发生这种情况,这将解决该问题!

Thank you all who took the time to waste it with me and help me! 谢谢所有花时间与我合作并帮助我的人! :) :)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM