Click here to Skip to main content
15,747,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm running from visual studio the case got passed. But while running the same from cmd prompt by using the dll file i'm facing this issue.

// Get the test data in table format and store it in DataRow type.
               DataTable DTExcelresult = IO.ReadExcel( excelLocation, methodName );
               for( int rowNo = 0; rowNo < DTExcelresult.Rows.Count; rowNo++)
                   // Represents a row of data in a DataTable.
                   System.Data.DataRow resultRows = DTExcelresult.Rows[rowNo];

                   // Return the tesdata in DataRow type.
                   yield return new Tuple<System.Data.DataRow>( resultRows );

               } // end for

in the first line of this code error happens. Anyone have idea what would be the problem.

What I have tried:

I was using a excel sheet to read the data
Updated 8-Mar-18 4:00am
ZurdoDev 8-Mar-18 9:41am    
Something is null.

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
Share this answer
Member 13715690 8-Mar-18 10:06am    
Thanks for your response, But when I run it in debugging mode the case got passed. Only when running through the cmd prompt it occurs
OriginalGriff 8-Mar-18 10:19am    
So you need to do it the old fashioned way: add logging statements to your code to narrow down exactly where the problem is, and which value is null. Then refine your logging code until you find out *why* it is null!

We can't do that for you!
Dave Kreskowiak 8-Mar-18 10:25am    
You're going to have to include some logging code in your code to find out why, if what you're saying about the first line of code generating the error is true, something in the ReadExcel method is null.

Your code seems to be assuming that every method call or properties works successfully and returns what you expect. Something isn't returning an object, but is instead returning null. You then try to use a method or property on the null object, and that's where you're getting the exception thrown.

We can't do any of this for you.
Member 13715690 8-Mar-18 10:18am    
 public class IO

        /// <summary>
        /// This method will initiate connection to open testdata document using OleDbConnection.
        /// </summary>
        /// <param name="excelPath">Path of the test data.</param>
        /// <returns>Returns the object initiated foe test data connection.</returns>
        private static OleDbConnection CreateExcelConnection(string excelPath)
            // Provides connection information as a string.
            string oleDbCon = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Jet OLEDB:Engine Type=5;Extended Properties=Excel 12.0;", excelPath);

            // An instance of the OleDbConnection class is initiated.
            return new OleDbConnection(oleDbCon);


        /// <summary>
        /// This method will read excel data.
        /// </summary>
        /// <param name="excelPath">Path of the test data.</param>
        /// <param name="methodName">Test method name.</param>
        /// <returns>Return test data value in DataTable format.</returns>
        public static DataTable ReadExcel(
                string excelPath, 
                string methodName)

            // 'using' block to dispose the object once the connection is no more needed.
            using (var excelconnection = CreateExcelConnection(excelPath))
                    string columnName = "S No";  // Column name 'Sno' is assigned.
                    string excelSheetName = methodName.Split('_')[1];  // Test sheet name for each case has been taken from method name.
                    // Opens the excel connection to read data required for the test method.

                    // Initiating the DataTable class.
                    DataTable DTExcelresult = new DataTable();

                    // Fetches the data from excel. 
                    OleDbDataAdapter DataAdapter = new OleDbDataAdapter(string.Format("Select * from [{0}$] where UCase([" + columnName + "]) IS NOT NULL ", excelSheetName), excelconnection);

                    // Stores the test data in a table format.
                    // Ends the excel connection.

                    // Retun the data in table format.
                    return DTExcelresult;


                catch (Exception exception)
                    // Write the exception to console.
                    Console.WriteLine("Exception", exception);

                    // Return null.
                    return null;




this is the IO class we are calling for retrieving data
OriginalGriff 8-Mar-18 10:48am    
Doesn't matter. the problem is a run time error - and it doesn't happen on your dev machine, so either it's specific to the target PC, only ever works on your PC, or it's an environment / setup / apps / configuration problem.
Regardless of which it is, we cannot replicate the problem so we can;t even start to look for it. The only person who can is you - so start logging what is happening and how it got there, and find out what is wrong / missing / null / broken. Until you know that, nothing can be reliably fixed. And the only way you have of finding out is ... to start logging exactly what is happening.

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