0

Error while reading data from excel,I was trying to set up a keyword driven framework from scratch, not completed but now iam getting wn error

I have put all classes below,Error is showing in "excelUtils class" and "Driverscript class "

Below is my error

Error

Exception in thread "main" java.lang.NullPointerException
    at Utility.excelUtils.getCellData(excelUtils.java:31)
    at executionengine.Driverscript.main(Driverscript.java:39)

This is Driver script Driverscript class

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 = "E://Jino_testing//Automation_project//Hybrid_framework-master//src//main//resources//dataengine//Zmarta.xls";

        //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;
                }
            }
        }
 }

This is excel Utils excelUtils class

package Utility;

import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;




public class excelUtils {

    private static HSSFWorkbook ExcelWBook;
    private static HSSFSheet ExcelWSheet;
    private static HSSFCell Cell;



    public static void setExcelFile (String path, String Sheetname) throws Exception {

        FileInputStream  ExcelFile = new FileInputStream (path);
        ExcelWBook = new HSSFWorkbook(ExcelFile);
        ExcelWSheet = ExcelWBook.getSheet(Sheetname);


    }


    public static String getCellData (int RowNum, int ColNum) {
        Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
        String CellData = Cell.getStringCellValue();
        return CellData;




    }


    }
jino
  • 69
  • 1
  • 9
  • This is **far** too much information. Please reduce this to [a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Max von Hippel Jul 17 '18 at 04:56
  • @MaxvonHippeli I pasted my whole code since i dont want to reduce infomations – jino Jul 17 '18 at 05:03
  • Well, unfortunately many people are unlikely to help you unless you do reduce the information. Anyway, reducing the information to a minimal example could lead you to find the solution on your own! It often does. – Max von Hippel Jul 17 '18 at 05:04
  • Read the documentation I linked to. – Max von Hippel Jul 17 '18 at 05:05
  • One of the two statements in `getCellData` is attempting to use a null pointer. Use your IDE debugger to stop in this method and examine the variables and figure out which one is null. Many of the POI methods return null if you attempt to retrieve objects that don't exist. For example, `getRow(int)` returns null if that row is not in the worksheet. – Jim Garrison Jul 17 '18 at 05:19
  • @JimGarrison Can you fix my code – jino Jul 17 '18 at 08:56
  • @jino : The issue is caused as the ___method.length___ in the __execute_Actions()__ method is __NULL__ . The ___Driverscript()___ is never invoked, Initialize __Driverscript()__ in your method. – Sijin Jul 17 '18 at 10:00
  • @Sijin not working – jino Jul 18 '18 at 04:58
  • @jino : Add `Driverscript script = new Driverscript();` on your main method and try. – Sijin Jul 18 '18 at 06:51
  • @Sijin Still same issues, havde edited my Driverscript class – jino Jul 18 '18 at 09:05

0 Answers0