1

Selenium - Keyword Driven Framework hereのチュートリアルに従っています。 Reflection APIについてよく知らない。 (私もJavaの初心者です)Nullポインタのリフレクションの使用時の例外

メインクラスを実行すると、NULLポインタ例外を受け取ります。私は何が間違っているのか分かりません。愚かな間違いかもしれません。私がここで紛失しているものを理解するのを助けてください。 は(私がキーワード駆動型フレームワークと初心者の観点からリフレクションAPIについてよりよく学ぶことができる場所誰かが私を導くことができる場合も、それは非常に参考になる。)

DriverScript:

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

} 
} 

ActionKeywords:

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:\\Ram\\MAC\\Se\\chromedriver.exe"); 
    driver = new ChromeDriver(); 
} 

public static void navigate(){ 
    driver.get("https://jira-dev.emdeon.net/secure/Dashboard.jspa"); 
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
    driver.manage().window().maximize(); 
} 

public static void enter_credentials(){ 
    driver.findElement(By.id("login-form-username")).sendKeys("RMuruganandam"); 
    driver.findElement(By.id("login-form-password")).sendKeys("[email protected]"); 
} 

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(); 
} 

} 

ExcelUtils:

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シート:

datasource.xlsx

+0

ために期待されるよう

これは良いアプローチではありませんが、私はコードの上だと思うが働く、あなたが例外を取得んスタックトレース – Alexey

+0

を添付してください? – Guy

答えて

1

としては、例外にした:ライン37上に、

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

methodがnullです。新しいDriverscriptオブジェクトを作成するためにコンストラクタを呼び出すことはありませんので、

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

ラインは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(); 
    } 
}