2

Hi guys im new to all this test automation stuff and trying to learn following a tutorial but im stuck trying to run this code.

I get an exception

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

dunno whats wrong as im following a tutorial so i assume everything should be working.

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;
                }
            }
        }
 }
Teriyaki
  • 31
  • 3
  • Which line is `DriverScript.java:45`? – Jens Mar 25 '15 at 08:17
  • for(int i=0;i – Teriyaki Mar 25 '15 at 08:19
  • When I copy your code into an editor; line 45 is the closing "{" of the main method. It does not make much sense to copy code here that is different from the code that creates this exception. Btw: the exception tells you that an object on which you invoke a method is NULL. And it gives you the line number. I think you should really learn something about essential Java basics before starting to deal with reflection (with reflection, one can make many subtle mistakes - it is for sure not something an absolute beginner should be looking into). – GhostCat Mar 25 '15 at 08:20
  • Does actionKeywords have any methods? There are multiple places where that exception can be thrown from. Line45 would be nice to know. Also, `throws Exception` made me throw up in my mouth. Don't do that. – Ron Thompson Mar 25 '15 at 08:20
  • @Teriyaki looks like method is null. – Jens Mar 25 '15 at 08:21
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Jens Mar 25 '15 at 08:37

2 Answers2

4

The problem is that you do never fill something into your method[] array. In the constructor, the array would be filled, but it is never called. Therefore, try calling the constructor inside the main method.

public static void main(String[] args) throws Exception {
    new DriverScript();
...
flogy
  • 906
  • 4
  • 16
  • Well, that is just the cure. The actual problem, frankly speaking, is more like: the whole code posted is one big compilation of bad java practices that should be avoided. ;-( – GhostCat Mar 25 '15 at 08:23
  • @EddyG the question was "how do I fix this problem?" not "how do I make this program better?" – user253751 Mar 25 '15 at 08:24
  • thanks flogy that worked. im juut following this guide http://www.toolsqa.com/selenium-webdriver/keyword-driven-framework/java-reflection-class/ its exact copy and paste so i dunno =/ – Teriyaki Mar 25 '15 at 08:27
  • @immibis Sure thing. But if the author doesn't understand a thing about Java programming, he will be coming back here 50 times today to ask us how to resolve the next problem in this pile of bad, ugly code. Lets use a bad car comparison: this guy looks to me like somebody who wants to take his first driving lesson in a Formula 1 racing car. But he fails to start the car; because he has no idea how to do that. My personal feeling is: only telling that guy how to start the engine will not help him in the long run. – GhostCat Mar 25 '15 at 08:29
  • @Teriyaki Seems that this tutorial comes with that mistake. Generally, as EddyG already mentionet, the code style in this tutorial is not very outstanding... I'd recommend you to look for another tutorial on Java Reflection and also take a look at some Java basics. – flogy Mar 25 '15 at 08:39
  • A true comment that would more fit into a discussion: if this code is really coming directly from a **tutorial**, then this code is in deed **outstanding**. Just not in a positive way ... – GhostCat Mar 25 '15 at 08:43
  • @flogy Thanks, your solution worked for me, even I faced same problem as Teriyaki in this tutorial. – Anonymous Aug 19 '15 at 10:53
0

In this line you need to change "Test Steps" to 'Sheet1' (or change the Excel sheet name to "Test Steps"):

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