2015-03-25 216 views
2

嗨,大家好我是新来的所有这些测试自动化的东西,并试图学习下面的教程,但我卡住试图运行此代码。使用Java反射类获取方法

我得到一个异常

Exception in thread "main" java.lang.NullPointerException 
    at executionEngine.DriverScript.execute_Actions(DriverScript.java:45) 
    at executionEngine.DriverScript.main(DriverScript.java:39) 

不知道什么是错的IM下面的教程,所以我认为一切都应该工作。

package executionEngine; 

import java.lang.reflect.Method; 
import config.ActionKeywords; 
import utility.ExcelUtils; 

public class DriverScript { 
    //This is a class object, declared as 'public static' 
    //So that it can be used outside the scope of main[] method 
    public static ActionKeywords actionKeywords; 
    public static String sActionKeyword; 
    //This is reflection class object, declared as 'public static' 
    //So that it can be used outside the scope of main[] method 
    public static Method method[]; 

    //Here we are instantiating a new object of class 'ActionKeywords' 
    public DriverScript() throws NoSuchMethodException, SecurityException{ 
     actionKeywords = new ActionKeywords(); 
     //This will load all the methods of the class 'ActionKeywords' in it. 
       //It will be like array of method, use the break point here and do the watch 
     method = actionKeywords.getClass().getMethods(); 
    } 

    public static void main(String[] args) throws Exception { 

     //Declaring the path of the Excel file with the name of the Excel file 
     String sPath = "D://Tools QA Projects//trunk//Hybrid Keyword Driven//src//dataEngine//DataEngine.xlsx"; 

     //Here we are passing the Excel path and SheetName to connect with the Excel file 
     //This method was created in the last chapter of 'Set up Data Engine'  
     ExcelUtils.setExcelFile(sPath, "Test Steps"); 

     //Hard coded values are used for Excel row & columns for now 
     //In later chapters we will use these hard coded value much efficiently 
     //This is the loop for reading the values of the column 3 (Action Keyword) row by row 
     //It means this loop will execute all the steps mentioned for the test case in Test Steps sheet 
     for (int iRow = 1;iRow <= 9;iRow++){ 
      //This to get the value of column Action Keyword from the excel 
      sActionKeyword = ExcelUtils.getCellData(iRow, 3); 
      //A new separate method is created with the name 'execute_Actions' 
      //You will find this method below of the this test 
      //So this statement is doing nothing but calling that piece of code to execute 
      execute_Actions(); 
      } 
     } 

    //This method contains the code to perform some action 
    //As it is completely different set of logic, which revolves around the action only, 
    //It makes sense to keep it separate from the main driver script 
    //This is to execute test step (Action) 
    private static void execute_Actions() throws Exception { 
     //This is a loop which will run for the number of actions in the Action Keyword class 
     //method variable contain all the method and method.length returns the total number of methods 
     for(int i = 0;i < method.length;i++){ 
      //This is now comparing the method name with the ActionKeyword value got from excel 
      if(method[i].getName().equals(sActionKeyword)){ 
       //In case of match found, it will execute the matched method 
       method[i].invoke(actionKeywords); 
       //Once any method is executed, this break statement will take the flow outside of for loop 
       break; 
       } 
      } 
     } 
} 
+0

哪条线是'DriverScript.java:45'? – Jens 2015-03-25 08:17:13

+0

for(int i = 0; i Teriyaki 2015-03-25 08:19:44

+0

当我将代码复制到编辑器中时;第45行是主要方法的结尾“{”。在这里复制代码与创建此异常的代码不同是没有意义的。 Btw:异常告诉你一个你调用方法的对象是NULL。它给你的行号。我认为在开始处理反思之前,你应该真正了解一些基本的Java基础知识(用反思,可以犯很多细微的错误 - 肯定不是绝对的初学者应该关注的东西)。 – GhostCat 2015-03-25 08:20:10

回答

4

问题是你永远不会在你的方法[]数组中填充某些东西。在构造函数中,数组将被填充,但它永远不会被调用。因此,请尝试在主方法内调用构造函数。

public static void main(String[] args) throws Exception { 
    new DriverScript(); 
... 
+0

那么,这只是治疗。坦率地说,实际的问题更像是:所发布的整个代码是应该避免的错误java实践的一个大汇编。 ;-( – GhostCat 2015-03-25 08:23:56

+0

@EddyG的问题是“我该如何解决这个问题?”而不是“我该如何让这个程序更好?” – immibis 2015-03-25 08:24:23

+0

感谢flogy的工作。im juut遵循本指南http://www.toolsqa.com/ selenium-webdriver /关键字驱动框架/ java-reflection-class /它的确切的复制和粘贴,所以我dunno =/ – Teriyaki 2015-03-25 08:27:40

0

在这一行,你需要改变"Test Steps"'Sheet1'(或更改Excel工作表名称"Test Steps"):

ExcelUtils.setExcelFile(sPath, "Test Steps");