使用Java反射类获取方法

使用Java反射类获取方法,java,selenium,reflection,Java,Selenium,Reflection,嗨,伙计们,我对所有这些测试自动化的东西都是新手,并试图学习下面的教程,但我一直在尝试运行这些代码 我有个例外 Exception in thread "main" java.lang.NullPointerException at executionEngine.DriverScript.execute_Actions(DriverScript.java:45) at executionEngine.DriverScript.main(DriverScript.java:39)

嗨,伙计们,我对所有这些测试自动化的东西都是新手,并试图学习下面的教程,但我一直在尝试运行这些代码

我有个例外

Exception in thread "main" java.lang.NullPointerException
    at executionEngine.DriverScript.execute_Actions(DriverScript.java:45)
    at executionEngine.DriverScript.main(DriverScript.java:39)
不知道什么是错误的,因为我下面的教程,所以我认为一切都应该工作

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;
                }
            }
        }
 }
包执行引擎;
导入java.lang.reflect.Method;
导入config.ActionKeywords;
导入utility.ExcelUtils;
公共类驱动程序脚本{
//这是一个类对象,声明为“publicstatic”
//这样就可以在main[]方法的范围之外使用它
公共静态关键词;
公共静态字符串sActionKeyword;
//这是反射类对象,声明为“公共静态”
//这样就可以在main[]方法的范围之外使用它
公共静态法[];
//在这里,我们将实例化一个类为“ActionKeywords”的新对象
public DriverScript()抛出NoSuchMethodException、SecurityException{
actionKeywords=新的actionKeywords();
//这将加载其中“ActionKeywords”类的所有方法。
//这将像数组的方法,使用断点在这里,并做手表
方法=actionKeywords.getClass().getMethods();
}
公共静态void main(字符串[]args)引发异常{
//用Excel文件名声明Excel文件的路径
String sPath=“D://Tools-QA-Projects//trunk//Hybrid-Keyword-Driven//src//dataEngine//dataEngine.xlsx”;
//这里我们将传递Excel路径和SheetName以连接Excel文件
//此方法是在“设置数据引擎”的最后一章中创建的
setExcelFile(sPath,“测试步骤”);
//硬编码值目前用于Excel行和列
//在后面的章节中,我们将更有效地使用这些硬编码值
//这是逐行读取第3列(操作关键字)值的循环
//这意味着这个循环将执行测试步骤表中提到的测试用例的所有步骤

对于(int iRow=1;iRow而言,问题在于您从不在方法[]数组中填充任何内容。在构造函数中,数组将被填充,但从未被调用。因此,请尝试在主方法中调用构造函数

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

在这一行中,您需要将
“测试步骤”
更改为
“Sheet1”
(或将Excel工作表名称更改为
“测试步骤”
):


哪一行是
DriverScript.java:45
?for(int i=0;i当我将代码复制到编辑器中时;第45行是结束“{”在这里复制与创建此异常的代码不同的代码没有多大意义。顺便说一句:异常告诉您调用方法的对象为NULL。它给出了行号。我认为在开始处理reflec之前,您应该真正了解一些基本Java基础知识反思(通过反思,一个人可能会犯许多微妙的错误——这肯定不是一个绝对的初学者应该研究的).actionKeywords有任何方法吗?可以从多个地方抛出异常。第45行很高兴知道。而且,
抛出异常
让我吐了。不要这样做。@Teriyaki看起来方法是空的。好吧,这就是解决方法。坦率地说,实际问题更像是:整个code posted是一个应该避免的糟糕java实践的大汇编。(@EddyG)问题是“我如何解决这个问题?”而不是“我如何使这个程序更好?”感谢flogy的工作。我只是按照这本指南进行复制和粘贴,所以我不知道=/@immibis是肯定的。但是如果作者对Java编程一无所知,他今天将50次回到这里,问我们如何解决这堆糟糕、难看的代码中的下一个问题。让我们用一个糟糕的汽车比较:这家伙看起来很糟糕我喜欢一个想在一级方程式赛车上上第一堂驾驶课的人。但是他没能启动赛车,因为他不知道如何启动。我个人的感觉是:从长远来看,只告诉那个家伙如何启动引擎对他没有帮助。@Teriyaki似乎这本教程也有这个错误。一般来说,就像EddyG一样提到,本教程中的代码风格不是很出色……我建议您寻找另一个关于Java反射的教程,并了解一些Java基础知识。
ExcelUtils.setExcelFile(sPath, "Test Steps");