簡體   English   中英

使用反射時出現空指針異常

[英]Null Pointer Exception while using Reflection

我正在關注 Selenium - Keyword Driven Framework here的教程。 我對反射 API 不是很熟悉。 (我也是 Java 的初學者)

當我執行我的主類時,我收到一個空指針異常。 我不確定我做錯了什么。 可能是一個愚蠢的錯誤。 請幫助我了解我在這里缺少什么。 (此外,如果有人可以指導我從初學者的角度更好地了解關鍵字驅動框架和反射 API,那將非常有幫助。)

驅動腳本:

package testdev;

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

public class DriverScript {

public static ActionKeywords actionKeywords;
public static String sActionKeyword;
public static Method method[];

public DriverScript() throws NoSuchMethodException, SecurityException{
    actionKeywords = new ActionKeywords();
    method = actionKeywords.getClass().getMethods();
}

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

    String sPath = "C://Users//testusr//workspace//src//datasource//datasource.xlsx";
    ExcelUtils.setExcelFile(sPath, "sheet");

    for(int i=1; i<=7; i++){
        sActionKeyword = ExcelUtils.getCellData(i, 3);
        execute_Actions();
    }
}

private static void execute_Actions() throws Exception{

    for(int j=0;j < method.length;j++){
        if(method[j].getName().equals(sActionKeyword)){
            method[j].invoke(actionKeywords);
            break;
        }
    }

}
}

操作關鍵字:

package config;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ActionKeywords {

public static WebDriver driver;

public static void openbrowser(){
    System.setProperty("webdriver.chrome.driver", "G:\\Usr\\MAC\\Se\\chromedriver.exe");
    driver = new ChromeDriver();
}

public static void navigate(){
    driver.get("URL");
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.manage().window().maximize();
}

public static void enter_credentials(){
    driver.findElement(By.id("login-form-username")).sendKeys("username");
    driver.findElement(By.id("login-form-password")).sendKeys("pswd");
}

public static void click_login(){
    driver.findElement(By.id("login")).click();
}

public static void wait_for(){
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}

public static void click_logout() throws InterruptedException{
    driver.findElement(By.id("header-details-user-fullname")).click();
    Thread.sleep(30);
    driver.findElement(By.id("log_out")).click();
}

public static void closebrowser(){
    driver.quit();
}

}

Excel實用程序:

package utility;

import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtils {

public static XSSFWorkbook ExcelWBook;
public static XSSFSheet ExcelWSheet;
public static XSSFCell Cell;
public static FileInputStream ExcelFile;

public static void setExcelFile (String Path, String SheetName) throws Exception{
    FileInputStream ExcelFile = new FileInputStream(Path);
    ExcelWBook = new XSSFWorkbook(ExcelFile);
    ExcelWSheet = ExcelWBook.getSheetAt(0);
}

public static String getCellData (int rowNum, int colNum) {
    String CellData ="";
    Cell = ExcelWSheet.getRow(rowNum).getCell(colNum);
    CellData= Cell.getStringCellValue();        
    return CellData;
}
}

例外:

java.lang.NullPointerException
at testdev.DriverScript.execute_Actions(DriverScript.java:37)
at testdev.DriverScript.main(DriverScript.java:28)

Excel表格:

數據源.xlsx

正如例外所說:在第 37 行;

for (int j = 0; j < method.length; j++) {

method為空。 因為你從不調用構造函數來創建一個新的 Driverscript 對象,所以

method = actionKeywords.getClass().getMethods();

line 從不為method賦值。

所有字段都是static但您正在嘗試在構造函數中分配它們的值。

這不是一個好方法,但我認為上面的代碼按您的預期工作

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

    String sPath = "C://Users//testusr//workspace//src//datasource//datasource.xlsx";
    ExcelUtils.setExcelFile(sPath, "sheet");

    for(int i=1; i<=7; i++){
        sActionKeyword = ExcelUtils.getCellData(i, 3);
        actionKeywords = new ActionKeywords();
        method = actionKeywords.getClass().getMethods();
        execute_Actions();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM