Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am getting this error but I don't know how to solve this

Object reference not set to an instance of an object.
VB
[NullReferenceException: Object reference not set to an instance of an object.]
   System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +24


I have a aspx page which contains a master page having telerik RadMenu whose datasource is defined in Masterpage cs file something like this

if (!IsPostBack)
{
C#
if (Session["MenuXML"] != null)
              xmlDataSource.Data = Session["MenuXML"].ToString();

}
}

xmlDataSource is the name of RadMenu is aspx file and I have stored xml in this session some thing like this

XML
<Menus>
      <Menu>
        <MenuID>3</MenuID>
        <Text>    Book</Text>
        <Navigate>#</Navigate>
        <Menu>
          <MenuID>51</MenuID>
          <Text>  Hotel Search</Text>
          <Navigate>Search.aspx</Navigate>
          <ParentID>3</ParentID>
        </Menu>
    <Menu>
    this tag repeating itself......
    </Menu>

</Menus>


My page works completely fine but as I do some random stuff by clicking a butting or paging the grid in aspx file then onlu some times I get this error not every time
Posted
Comments
Kornfeld Eliyahu Peter 30-Apr-14 4:03am    
Try step-by-step debug and see which variable is null...
Member 8680235 30-Apr-14 4:44am    
I did try to debug step by step by pressing F10 in VS-2010 but it passes through all the events and then page shows this error therefore I am unable to get where the error is thrown
[no name] 30-Apr-14 9:04am    
You need to debug using a debug build of your website.
Member 8680235 30-Apr-14 9:13am    
I am debugging in debug mode not in Release mode but still it does not throws exception in VS-2010 but page show this exception
Kornfeld Eliyahu Peter 30-Apr-14 10:05am    
I saw from one of your response that you are using telerik's components. Try telerik's support - they are good...

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 
Comments
Member 8680235 30-Apr-14 4:44am    
I did try to debug step by step by pressing F10 in VS-2010 but it passes through all the events and then page shows this error therefore I am unable to get where the error is thrown
Sergey Alexandrovich Kryukov 30-Apr-14 9:06am    
I wrote a whole small article for you to explain how to do it not hitting F10 million times... Did you even read it?
—SA
Member 8680235 30-Apr-14 9:18am    
My Friend Visual studio is not showing any exception in code but the page throws the exception which say object reference not set therefore I cannot


"You need to execute the application under debugger allowing handling exceptions during debug. It will stop your application on the line where the exception is thrown"
Sergey Alexandrovich Kryukov 30-Apr-14 9:33am    
The title of your question is the exception message. How can you tell us that it's "not showing any exception"?
What was showing that exception?
—SA
Member 8680235 30-Apr-14 9:48am    
Exception is not generated or debugger does not show any exception in VS-2010 but exception shows only in browser page gets explode but not in visual studio
Hi,

Debug using F11 was a better choice in this case, It'll look into the inner functions which causes this error. This error occurs when you try to use a disposed object or one of your object didn't properly initialised. Debug line by line, you'll able to capture the root cause.

Regards,
BlueSathish
 
Share this answer
 
Comments
Member 8680235 30-Apr-14 6:09am    
I did try to debug step by step by pressing F10 and F11 in VS-2010 but it passes through all the events and then page shows this error therefore I am unable to get where the error is thrown
Member 8680235 30-Apr-14 6:11am    
Visual studio does not shows any exception but page shows the exception can you please tell me where the error is generated using stack trace??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900