2016-05-01 21 views
-1

以下のコードでNULLポインタエラーが発生します。コードを実行すると、コードを実行する前に 'captureScreenshot()'関数を記述したコードの直前にすべてのコードが実行されます。コードの実行中にNULLポインタ例外が発生する

package rough; 

import java.io.File; 
import java.io.IOException; 
import java.util.Calendar; 
import java.util.GregorianCalendar; 
import java.util.concurrent.TimeUnit; 

import org.apache.commons.io.FileUtils; 
import org.openqa.selenium.By; 
import org.openqa.selenium.OutputType; 
import org.openqa.selenium.TakesScreenshot; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 



public class TestEmailAndScreenshot1 { 

public static WebDriver dr; 
public static String mailscreenshotpath; 

public static void captureScreenshot(){ 


    Calendar cal = new GregorianCalendar(); 
    int month = cal.get(Calendar.MONTH); 
    int year = cal.get(Calendar.YEAR); 
    int sec = cal.get(Calendar.SECOND); 
    int min = cal.get(Calendar.MINUTE); 
    int date = cal.get(Calendar.DATE); 
    int day = cal.get(Calendar.HOUR_OF_DAY); 


    File scrFile = ((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE); 
    try { 
     mailscreenshotpath =   System.getProperty("user.dir")+"\\screenshot\\"+year+"_"+date+"_"+(month+1)+"_"+day+"_"+min+"_" +sec+".jpeg"; 
     FileUtils.copyFile(scrFile, new File(mailscreenshotpath)); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 

     } 
} 



public static void main(String[] args) { 

    WebDriver dr=new FirefoxDriver(); 
    dr.manage().window().maximize();  
    dr.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS); 
    dr.get("http://gmail.com"); 
    dr.findElement(By.id("Email")).sendKeys("vijenderchhoker92"); 
    captureScreenshot(); 
System.out.println(" Screenshot taken successfully...."); 

} 

} 

答えて

0

あなたはmain

WebDriver dr = new FirefoxDriver(); 

を行うとcaptureScreenshot()方法で、それはまだnullですのであなたは、public static WebDriver dr;変数を隠し、あなたはエラーがそれを使用しようとしてもらいます。

public static void main(String[] args) { 
    dr = new FirefoxDriver(); 
    dr.manage().window().maximize(); 
    //... 
} 
に変更してください
関連する問題