Object reference not set to an instance of an object error in SharePoint
Object reference not set to an instance of an object is one of the generic and common in SharePoint, specially has been raised while working with List and libraries. Also it could raise while working with web part. The basic cause is the reference the object is not able to trace out by the SharePoint object mode.
Let me share with one simple example from event receiver perspective. I have written a simple event receiver in SharePoint where I’m selecting the value from a list column (i.e. Tipologi KPI in this example).
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
string CategoryName = string.Empty;
try
{
CategoryName = properties.AfterProperties["Tipologi KPI"].ToString();
}
catch (Exception ex)
{
properties.ErrorMessage = ex.Message.ToString();
properties.Cancel = true;
}
…..
I got the following error at very initial.
Object reference not set to an instance of an object. at Microsoft.SharePoint.Library.SPRequestInternalClass.PutFile(String bstrUrl, String bstrWebRelativeUrl, Object varFile, PutFileOpt PutFileOpt, String bstrCreatedBy, String bstrModifiedBy, Int32 iCreatedByID, Int32 iModifiedByID, Object varTimeCreated, Object varTimeLastModified, Object varProperties, String bstrCheckinComment, UInt32& pdwVirusCheckStatus, String& pVirusCheckMessage)
at Microsoft.SharePoint.Library.SPRequest.PutFile(String bstrUrl, String bstrWebRelativeUrl, Object varFile, PutFileOpt PutFileOpt, String bstrCreatedBy, String bstrModifiedBy, Int32 iCreatedByID, Int32 iModifiedByID, Object varTimeCreated, Object varTimeLastModified, Object varProperties, String bstrCheckinComment, UInt32& pdwVirusCheckStatus, String& pVirusCheckMessage)
Started debugging and again found the error at line properties.AfterProperties["Tipologi KPI"].ToString()
I have tried to using the following way.
SPList _List = properties.OpenWeb().Lists[properties.ListTitle];
SPListItem item = _List.GetItemById(properties.ListItemId);
CategoryName = item["Tipologi KPI"].ToString();
Same error has been occurred (custom error).
Server Error in ‘/’ Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.InteropServices.COMException: Object reference not set to an instance of an object.
Source Error:
|
|
Stack Trace:
[COMException (0x81020089): Object reference not set to an instance of an object.]
Microsoft.SharePoint.Library.SPRequestInternalClass.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bMigration, Boolean bPublish) +0
Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bMigration, Boolean bPublish) +411
[SPException: Object reference not set to an instance of an object.]
Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bMigration, Boolean bPublish) +534
Microsoft.SharePoint.SPListItem.AddOrUpdateItem(Boolean bAdd, Boolean bSystem, Boolean bPreserveItemVersion, Boolean bNoVersion, Boolean bMigration, Boolean bPublish, Boolean bCheckOut, Boolean bCheckin, Guid newGuidOnAdd, Int32& ulID, Object& objAttachmentNames, Object& objAttachmentContents, Boolean suppressAfterEvents) +3021
Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents) +682
Microsoft.SharePoint.SPListItem.UpdateOverwriteVersion() +182
Microsoft.SharePoint.WebControls.SaveButton.SaveItem(SPContext itemContext, Boolean uploadMode, String checkInComment) +256
Microsoft.SharePoint.WebControls.SaveButton.SaveItem() +104
Microsoft.SharePoint.WebControls.SaveButton.OnBubbleEvent(Object source, EventArgs e) +476
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +70
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981
|
Version Information: Microsoft .NET Framework Version:2.0.50727.4200; ASP.NET Version:2.0.50727.4016
While rechecking the code with column name, I found the typo mistake the column name should be Tipologia KPI instead of Tipologi KPI( one a was not there in the column reference). Rebuild the solution and this time it worked.
This error could be occured due to existing of multiple DLL in the GAC (may be for release and debug) by same name. In this case, remove the DLL from GAC and do a fresh build and place the new DLL in GAC. Make sure you reset the IIS (start >> run >> resetIIS) before trying again.
Please note, this error is not for list and libraries, as I said at the begining, the root cause is reference not found. So there could be multiple reasons behind this and as a consiquence there would be multiple solutions for different reasons as well. I have face this error for the above mentioned problem and get the resolutions by folloing the steps. Hope this will help you.
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
string CategoryName = string.Empty;
try
{
CategoryName = properties.AfterProperties["Tipologi KPI "].ToString();
}
catch (Exception ex)
{
properties.ErrorMessage = ex.Message.ToString();
properties.Cancel = true;
}


