Object reference not set to an instance of an object.

I created a master page, and put 3 TreeView Controls on the page. I binded each of the treeView controls to 3 XmlDataSources. I keep getting the below error message, but I don't have any idea what is going on. I tried to trap the error in the code behind, but the error is not getting thrown in the application. I believe from the stack trace it has something to do with the binding of the xml data source? Any suggestions would be greatly appreciated.

 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.]
   System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +39
   System.Xml.XmlDocument.Load(XmlReader reader) +159
   System.Web.UI.WebControls.XmlDataSource.PopulateXmlDocument(XmlDocument document, CacheDependency& dataCacheDependency, CacheDependency& transformCacheDependency) +388
   System.Web.UI.WebControls.XmlDataSource.GetXmlDocument() +204
   System.Web.UI.WebControls.XmlHierarchicalDataSourceView.Select() +42
   System.Web.UI.WebControls.TreeView.DataBindNode(TreeNode node) +479
   System.Web.UI.WebControls.TreeView.PerformDataBinding() +136
   System.Web.UI.WebControls.HierarchicalDataBoundControl.PerformSelect() +93
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
   System.Web.UI.WebControls.TreeView.DataBind() +24
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +92
   System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +33
   System.Web.UI.WebControls.TreeView.OnPreRender(EventArgs e) +183
   System.Web.UI.Control.PreRenderRecursiveInternal() +148
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Control.PreRenderRecursiveInternal() +233
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4440
LVL 6
LeeHenryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Elvio Lujan.Net Senior DeveloperCommented:
may be you can run it in debug mode to see the error details
0
aki4uCommented:
can you post the code how you use Load for XmlDocument?
0
LeeHenryAuthor Commented:

When I run it in debug mode, i can't trap the error, because it is not being thrown in any of the code behind files.

aki4u,


  private void Refresh_XML_Data_Source(XmlDataSource myXmlDataSource, string myXmlData)
    {
        myXmlDataSource.EnableCaching = false;
        myXmlDataSource.Data = myXmlData;
    }
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Elvio Lujan.Net Senior DeveloperCommented:
and... can make a break point to see the "myXmlDataSource" value
>>myXmlDataSource.EnableCaching = false; <-- in this line
0
aki4uCommented:
that is probably because myXmlDataSource is null.


try this:

            private void Refresh_XML_Data_Source(XmlDataSource myXmlDataSource, string myXmlData)
            {
                  if (!(XmlDataSource == null))
                  {
                        myXmlDataSource.EnableCaching = false;
                        myXmlDataSource.Data = myXmlData;
                  }
            }
0
LeeHenryAuthor Commented:
aki4u,

This didn't fix the problem. I'm getting the error after the page unloads...
??
0
aki4uCommented:
same error? if not, can you post the error description?
0
LeeHenryAuthor Commented:
yes.. same error..
0
aki4uCommented:
can you get the line where it fails?
same error means that you some of your objects are null and you are trying to use them.
try doing the same as I posted...e.g

if (!(object== null))
{
//your code here
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LeeHenryAuthor Commented:
figured it out.. The problem was on the Page Init when I loaded the master pages dynamically, I was doing a postback Check, and when the page unloaded it was crashing because the master page wasn't assigned. I removed the postback check, and it works.

Thanks for the suggestions..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.