Help_while执行java反射线程“main”中的异常java.lang.NullPointerException

时间:2016-01-01 06:49:47

标签: java

我正在使用关键字驱动的框架,Method []方法没有按预期进行处理。

代码明细:

package executionEngine;

import java.lang.reflect.Method;

import org.apache.commons.logging.impl.Log4JLogger;
import org.openqa.selenium.*;

import com.gargoylesoftware.htmlunit.WebConsole.Logger;

import utility.ExcelUtils;
import config.ActionKeywords;

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 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();
            System.out.println(method.length);
        }

        public static void main(String[] args) throws Exception 
        {
            //Declaring the path of the Excel file with the name of the Excel file
            String sPath = "C://Users//m1021704//Desktop//SeleniumProjwrk//KeywordDrivenFramework//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, "Sheet1");

            //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
                System.out.println(method[i]);
                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;
                    }
                }
            }
}

输出:

   the row,column is :( 1,3)has value in excel :openBrowser
   Exception in thread "main" java.lang.NullPointerException
        at executionEngine.DriverScript.execute_Actions(DriverScript.java:64)
        at executionEngine.DriverScript.main(DriverScript.java:52)

在运行它时我得到了null异常,它在execute_Actions()中抛出异常。请帮我解决这个问题。

0 个答案:

没有答案