Akshay

Akshay

  • NA
  • 82
  • 46.9k

error:Object reference not set to an instance of an object.

Nov 7 2014 2:51 AM
in simple web part i write following code


        protected override void CreateChildControls()
        {

             using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                 SPContentType contype=site.RootWeb.ContentTypes["SYFLocation"];

                 IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(contype);
                 foreach (SPContentTypeUsage uses in usages)
                 {
                     if (uses.IsUrlToList)
                     {
                         SPList list = web.GetList(uses.Url);
                         SPQuery qry = new SPQuery();
                         qry.Query = "<Where>"+
                             "<IsNotNull>"+
                             "<FieldRef Name='Title'/>"+
                                 "</IsNotNull>"+
                                 "</Where>";

                         SPListItemCollection itemcoll = list.GetItems(qry);
                         HtmlGenericControl ulTab = new HtmlGenericControl("ul");
                         using (ulTab)
                    {
                        foreach (SPListItem item in itemcoll)
                        {

                            if (itemcoll.Count > 0)
                            {
                                var containerDiv = new HtmlGenericControl("div");
                                HtmlControl liItem = BindHtml(item);
                                ulTab.Controls.Add(liItem);
                                containerDiv.Controls.Add(ulTab);
                                this.Controls.Add(containerDiv);


                            }
                            else
                            {
                                HtmlGenericControl errorDiv = new HtmlGenericControl("div");
                                HtmlGenericControl h2 = new HtmlGenericControl("h2");
                                h2.InnerText = "There are no items in Calender list";
                                errorDiv.Controls.Add(h2);
                                Controls.Add(errorDiv);

                            }
                        }
    
                    }


                     }
                 }
                }
            }
        }

        private HtmlControl BindHtml(SPListItem item)
        {

            HtmlGenericControl liItem = new HtmlGenericControl("li");
        
            HtmlAnchor link = new HtmlAnchor();
            string url = item["URL"] != null && item["URL"].ToString() != string.Empty ? item["URL"].ToString() : "#";
            string[] urls = url.Split(',');
            link.HRef = urls[0].Trim(' ');
            link.Target = "blank";
            link.InnerText = item["Title"] != null && item["Title"].ToString() != string.Empty ? item["Title"].ToString() : "";
            link.InnerHtml = item["SYFLocation"] != null && item["SYFLocation"].ToString() != string.Empty ? item["SYFLocation"].ToString() : "";
         
            liItem.Controls.Add(link);
            return liItem;
        }
    }






when i deploy web part after when i try to use webpart on oage it display error
faceing error like:
 

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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]    Microsoft.SharePoint.SPContentTypeUsage.GetUsages(SPContentType contentType) +56    Syncranet.Webparts.LocationResources.LocationResources.CreateChildControls() +476    System.Web.UI.Control.EnsureChildControls() +145    System.Web.UI.Control.PreRenderRecursiveInternal() +60    System.Web.UI.Control.PreRenderRecursiveInternal() +223    System.Web.UI.Control.PreRenderRecursiveInternal() +223    System.Web.UI.Control.PreRenderRecursiveInternal() +223    System.Web.UI.Control.PreRenderRecursiveInternal() +223    System.Web.UI.Control.PreRenderRecursiveInternal() +223    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3393 


Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5483

Answers (2)