C# Object reference not set to an instance of an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DarkArchon
    New Member
    • Mar 2008
    • 11

    C# Object reference not set to an instance of an object

    I seem to have a recurring problem in VS2008 using the xaml designer. For some reason in my wpf code the error "Object reference not set to an instance of an object" will appear on one of the lines that has nothing wrong with it. After reloading the designer the problem moves to another line. After a couple more reloads, the designer shows the window properly. The problem would seem to be that I never assign the components I'm using, but it doesn't seem to be the case. If anyone has any ideas it would be greatly appreciated1
  • LoanB
    New Member
    • Nov 2007
    • 62

    #2
    Use a try/catch to catch the exception then pass the exception to something like this method:

    Code:
              private void SHOWEXCEPTION(Exception e)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
    
    
                sb.Append("Messages");
                Exception wex = e;
                while (wex != null)
                {
                    sb.AppendFormat("[{0}]:{1}\n",wex.GetType().Name,wex.Message);
                    wex = wex.InnerException;
                }
    
                sb.Append("Stack Traces");
                wex = e;
                while (wex != null)
                {
                    sb.AppendFormat("[Type:{0}]; StackTrace:{1}\n", wex.GetType().Name, wex.StackTrace);
                    sb.Append("\n");
                    wex = wex.InnerException;
                }
    
                wex = e;
                while (wex != null)
                {
                    if (wex is ServiceException)
                    {
                        ServiceException swex = (ServiceException)wex;
                        foreach (string msg in swex.Details)
                        {
                            sb.AppendFormat("[Service Exception details: {0}]\n", msg);
                        }
                    }
                    sb.Append("\n");
                    wex = wex.InnerException;
                }
    
                MessageBox.Show(" An Error has occurred:\n\n" +sb.ToString());
            }
    You may need to change it but the goal is to try and get a better description of what is null.

    Comment

    Working...