Developer forum

Forum » Development » Indexbuilder null object reference on getPageById

Indexbuilder null object reference on getPageById

Justin Breg
Reply

Hi Dynamicweb,

We've got an IndexBuilder where we want to get the Page by the PageID.

However the method 'GetPageById' returns a null object reference when we pass an existing pageID in the method.

Here's our error:

Object reference not set to an instance of an object. at Dynamicweb.Frontend.ApplicationLoad.get_PageCache() at Dynamicweb.Content.Page.GetPageById(Int32 id, Boolean force) at Dynamicweb.Content.Page.GetPageById(Int32 id) at Bluedesk.FullTextIndex.FullTextIndexBuilder.Build(IIndexWriter writer, Tracker tracker) in C:\vso\Alkmaar Marketing\Tools\Bluedesk.FullTextIndex\FullTextIndexBuilder.cs:line 177 at Dynamicweb.Indexing.Index.Build(String instance, String name, Tracker tracker) at Dynamicweb.Indexing.IndexService.<>c__DisplayClass1.b__0(Tracker tracker)

And here's the code:

var thispage = Dynamicweb.Content.Page.GetPageById(pageID);
                                    if (thispage != null && thispage.Parent != null)
                                    {
                                        var parentpage = thispage.Parent;
                                        var categoryName = parentpage.ItemType.Equals("EventCategory") ? parentpage.MenuText : parentpage.Parent.ItemType.Equals("EventCategory") ? parentpage.Parent.MenuText : "";
                                        doc.Add("CategoryName", categoryName);
                                    }

Best regards,

Justin.


Replies

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply
This post has been marked as an answer

Hi Justin,

looks like there is no availbale System.Web.HttpContext.Current in scope when your BuildIndex method is executed. So you can try to get the page by uisng:

Dynamicweb.Content.Page.GetPageBySql(String.Format("SELECT * FROM [Page] WHERE [PageId] = '{0}'", pageID))
Regards, Dmitrij

Votes for this answer: 1
 
Justin Breg
Reply

Hi Dmitriy,

The SQL statement gives us the same null object reference. If we query directly in the database with the same SQL statement, we do receive our desired result.

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

HI Justin,
could  you add a reference to the System.Web in your custom project and check if you will get the non nullable System.Web.HttpContext.Current object when building your index?
Regards, Dmitrij

 
Justin Breg
Reply

The system.Web.HttpContext.Current object is indeed null when building the index.

 
Nicolai Pedersen
Reply

Hi Justin

I am affraid that the Content.Page object relies indirectly on httpcontext, so you cannot use the object it self. You have to use the raw database values. On DW9 that should be solved...

BR Nicolai

 

You must be logged in to post in the forum