Click here to Skip to main content
15,888,277 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am not able to run even a sample aneka (C#) code which probably doesn't have any error? I am unable to figure out the exact problem.

It will be very helpful if somebody who can throw some light on it.

Thanks in Advance.
Posted
Comments
[no name] 17-Mar-15 13:07pm    
The error (exception) message you quote is a very common one. It basically means you're trying to use a variable that should reference some object but you haven't actually assigned an object to it yet. For a concrete answer in your case you need to provide the part of your source code where the exception is occuring.
Sergey Alexandrovich Kryukov 17-Mar-15 15:56pm    
Nevertheless, I answered and advised what to do, please see. Indeed, this problem is one of the easiest.
—SA
Sergey Alexandrovich Kryukov 17-Mar-15 15:54pm    
What, without any code sample? I can only give you general instructions, but it should be enough for you. You will easily locate and fix it yourself.
—SA

1 solution

Not only you did not show where the exception with the message "Object reference not set to an instance of an object" is thrown, you did not even show any code sample. Do you think it's logical?

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
[no name] 17-Mar-15 16:00pm    
Page 3. +5
Sergey Alexandrovich Kryukov 17-Mar-15 16:29pm    
Thank you, Sebastian.
—SA
Member 11533009 18-Mar-15 8:41am    
Thank you very much SA. That was really helpful.
I got the program part which gives the error through stack trace.
Sergey Alexandrovich Kryukov 18-Mar-15 11:11am    
You are welcome.
—SA
Member 11533009 18-Mar-15 8:42am    
But another problem is that the same program when I run it through VS command prompt, it gives me a list of errors related to references.
i.e. The type or namespace could not be found for many objects/ functions.
I tried to solve it by adding the required reference, but it seems to be already added.

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

  Print Answers RSS


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