Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
An unhandled exception of type 'System.NullReferenceException' occurred in MyProject.exe
Additional information: Object reference not set to an instance of an object.
It says ,
StringCollection strCol is null,
but table.Script() is not null(it includes the record.)
Here my codelines

C#
foreach (var item in Sourceclb.Items)
           
            {
                Table table = database.Tables[item.ToString()];
            StringCollection strCol = table.Script();//Gives exception here
            var script = "";
            foreach (var key in strCol)
            {
                script += key;

            }
            command.Connection = ttbl;
       command.CommandText = "USE "+_hedefDb+" \n EXEC sp_sqlexec '"+scriptdondur(script)+ "'";
                
            command.ExecuteNonQuery();
            txtLog.AppendText("*TABLE COPIED* "+item.ToString()+" has been copied. \r\n");
            }
Posted

The first thing to do is put a breakpoint on the line:
C#
StringCollection strCol = table.Script();
And look at the table variable - my guess it that it is null, which means that when you try to access any method or property you will get that error.

So then, look at why.
What is the value of item?
What does item.ToString() return? Is it a known Table in your database.Tables collection?

I'm betting that item.ToString() doesn't return what you think it does...
 
Share this answer
 
Comments
Cenkay Vergili 2-Dec-13 10:16am    
When I delete the breakpoint foreach works for only once. It works succesfully only one time then gives the exception but when I put a breakpoint to the line you told me. It does not even work for once. :<
and item.ToString() returns a known table name in my database."SYS_MyTable"
It cannot be as described. The line where the exception is thrown (if I can assume it is thrown actually in this line, which is not the 100% proven fact to me) clearly says that left reference-type value on left is assigned to the property value on right. Assigning null to null would not throw exception, and the left reference before assignment does not matter. If the exception was thrown in this line, it could be only one reason: table is null, and, when you try to dereference it by addressing its Script method, it throws the said exception. In turn, table can easily become null. Look at the previous line. The indexed property database.Tables is the associative container (dictionary) indexed by string keys. Usually, returning null is the way to indicate that the value object (a table in this case) could not be found by this key. Check up the string value of the key, it must be the name which is not the valid table name in your database. That should be a root reason for the problem. The case looks pretty clear.

So, you did not correctly 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
Cenkay Vergili 2-Dec-13 10:35am    
Thank you very much for this solution! It worked!
After wasting my hours and could not even think It was because the table was null.
(I am busy this year . I am trying to study to win a good University and also working.)"it could be only one reason: table is null"
That saved me for today.
Thank you very much.
Sergey Alexandrovich Kryukov 2-Dec-13 10:41am    
That's great. You are very welcome.
Try to learn the methods or work, not just this particular case.
Good luck, call again.
—SA

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