[英]System.NullReferenceException: „Object reference not set to an instance of an object.” problem
[英]System.NullReferenceException: Object reference not set to an instance of an object. During Test automation execution
如果有人幫助我解決我在自動化執行過程中面臨的以下問題,那就太好了:
我正在嘗試根據 excel 中的行數引入測試用例循環。 我在 Visual Studio 中使用數據驅動功能來實現這一點。 當我在“測試資源管理器”中執行時,這在 Visual Studio 環境中完美運行。 但是,當我構建這個項目的 DLL 並復制到自動化工具的工作文件夾(它選擇執行的位置)時,會觸發以下錯誤:
System.Reflection.TargetInvocationException:調用的目標已拋出異常。 ---> System.NullReferenceException:未將對象引用設置為對象的實例。
請看下面的代碼片段:
[DataSource("System.Data.Odbc", "Dsn=Excel Files;Driver={Microsoft Excel Driver (*.xlsx)};dbq=J:\\Automation_Working_Directory\\Automation_Files_Demo\\Test_Report_01Base.xlsx;defaultdir=.;driverid=790;maxbuffersize=2048;pagetimeout=5;readonly=true", "SrsReportRunController_Dialog$", DataAccessMethod.Sequential)]
[TestMethod]
public void Test_Report_01()
{
#region Fetch data from Excel file
string fromDate = TestContext.DataRow["From date"].ToString();
string startDate = TestContext.DataRow["Report period start date"].ToString();
string toDate = TestContext.DataRow["To date"].ToString();
#endregion Fetch data from Excel file
Application.Control.TextBox().SetValue(fromDate);}
當我嘗試使用“fromDate”值輸入應用程序時,我收到 NullReferenceExecption。
提前致謝。
首先,您應該在代碼中添加 try-catch,這將幫助您確定確切的錯誤行號
我認為在 DataRow 值為null
情況下, ToString()
可能會導致此問題。
嘗試使用Convert Class這將有助於在一定程度上避免異常
public void Test_Report_01()
{
try{
#region Fetch data from Excel file
string fromDate = Convert.ToString(TestContext.DataRow["From date"]);
string startDate = Convert.ToString(TestContext.DataRow["Report period start date"]);
string toDate = Convert.ToString(TestContext.DataRow["To date"]);
#endregion Fetch data from Excel file
Application.Control.TextBox().SetValue(fromDate);
}
catch(Exception e){
Console.WriteLine(e.toString());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.