怎么会抛出NullReferenceException?

时间:2011-02-22 16:58:30

标签: c# .net nullreferenceexception

我有以下代码:

EventLog el1 = new EventLog();
el1.Log = "Application";
el1.Source = "SharePoint Foundation";

el1.WriteEntry("Start", EventLogEntryType.Information);
el1.WriteEntry("SPContext : " + (SPContext.Current == null ? "nothing" : "something"), EventLogEntryType.Information);
el1.WriteEntry("Web ID: " + (SPContext.Current.Web == null ? "nothing" : "something"), EventLogEntryType.Information);

前两个WriteEntry日志就好了,第二个记录了“东西”。但第三个将永远错误。我的大脑刚刚被炸掉,或者这种情况从未发生过?

请注意: 第二个WriteEntry向日志写入“某事”。这意味着SPContext.Current不为空。

更新

我不知道它是否有所作为,但我使用SPSecurity.RunWithElevatedPrivileges以提升的权限运行它。这是我的代码:

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            try
            {
                EventLog el1 = new EventLog();
                el1.Log = "Application";
                el1.Source = "SharePoint Foundation";
                el1.WriteEntry("Start", EventLogEntryType.Information);
                el1.WriteEntry("SPContext : " + (SPContext.Current == null ? "nothing" : "something"), EventLogEntryType.Information);
                el1.WriteEntry("Web ID: " + (SPContext.Current.Web == null ? "nothing" : "something"), EventLogEntryType.Information);

            }
             catch (Exception ex)
            {
                EventLog el1 = new EventLog();
                el1.Log = "Application";
                el1.Source = "SharePoint Foundation";
                el1.WriteEntry(ex.Message + System.Environment.NewLine + ex.StackTrace, EventLogEntryType.Error);
                throw ex;
            }
      });

我的堆栈跟踪:

Object reference not set to an instance of an object.
   at Copy_Special.CrossSiteCopy.<>c__DisplayClass1.<Execute>b__0()

仅供参考,此代码是SharePoint自定义工作流操作的一部分......

2 个答案:

答案 0 :(得分:2)

更新:根据问题中提供的新说明以及此答案的评论完全重写我的答案。

  1. 最后一行至少应如下所示:

    SPContext.Current == null || SPContext.Current.Web == null ? "nothing" : "something"
    
  2. SPContext.Web属性包含一个相当复杂的逻辑,在某些情况下会创建一个新的SPWeb实例。因此,很可能,它可能会失败NullReferenceException,尽管没有记录该行为。在许多情况下,它也会产生InvalidOperationException

  3. 在提升的权限下,没有有效的SPContext.Current。您必须再次打开网站 以检索可在不同安全上下文下工作的SPWeb实例。

  4. 正确权限提升的示例代码:

    // site and web objects working with the current user's privileges
    SPSite userSite = SPContext.Current.Site;
    SPWeb userWeb = SPContext.Current.Web;
    
    // elevate privileges
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        // get new site and web objects working with elevated privileges
        using (SPSite elevatedSite = new SPSite(userSite.ID)) 
        {
            using (SPWeb elevatedWeb = ElevatedsiteColl.OpenWeb(userWeb.ID)) 
            {
                // …code using elevatedSite and elevatedWeb…
            }
        }
    });
    

答案 1 :(得分:0)

试试这个:

EventLog el1 = new EventLog();
el1.Log = "Application";
el1.Source = "SharePoint Foundation";
el1.WriteEntry("Start", EventLogEntryType.Information);
var c = SPContext.Current;
el1.WriteEntry("SPContext : " + (c == null ? "nothing" : "something"), EventLogEntryType.Information);
if (c != null) el1.WriteEntry("Web ID: " + (c.Web == null ? "nothing" : "something"), EventLogEntryType.Information);