Shafi S
Shafi S

Reputation: 25

Getting java.lang.NullPointerException , while running the code

public class TestMeetings extends Base_Page{

public static WebDriver admin, participant;
public ExtentHtmlReporter htmlReporter;
public ExtentReports extent;
public ExtentTest test;
File src;
FileInputStream objfile ;
Properties obj;
String url;
Robot r;
Actions action;

@BeforeTest
public void beforeTest_Admin() throws IOException, AWTException
{
    
    htmlReporter = new ExtentHtmlReporter("./reports/extent.html");
    htmlReporter.config().setEncoding("utf-8");
    htmlReporter.config().setDocumentTitle("Automation Report");
    htmlReporter.config().setReportName("Automation Test Results");
    htmlReporter.config().setTheme(Theme.STANDARD);
    
    extent = new ExtentReports();
    extent.attachReporter(htmlReporter);
    
    extent.setSystemInfo("Automation Tester","XXXXX");
    extent.setSystemInfo("Organization","XXXXX");
            
    r = new Robot();
    action = new Actions(admin);
    src = new File("C:\\Users\\DELL\\eclipse-workspace\\XxxxOnlineDemo\\Object_Repo.properties");                    
    objfile = new FileInputStream(src);
    obj = new Properties();
    obj.load(objfile);
    
    Map prefs = new HashMap();
    prefs.put("profile.default_content_setting_values.notifications", 1);
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--use-fake-ui-for-media-stream");
    options.setExperimentalOption("prefs", prefs);
    System.setProperty("webdriver.chrome.driver", "E:\\drivers\\chromedriver.exe");
    admin = new ChromeDriver(options);
    admin.manage().window().maximize();

}

@BeforeTest
public void beforeTest_participant()
{
    System.out.println("before test of participant is running");
    
    Map prefs1 = new HashMap();
    prefs1.put("profile.default_content_setting_values.notifications", 1);
    ChromeOptions options1 = new ChromeOptions();
    options1.addArguments("incognito");
    options1.addArguments("--use-fake-ui-for-media-stream");
    options1.setExperimentalOption("prefs", prefs1);
    System.setProperty("webdriver.chrome.driver", "E:\\drivers\\chromedriver.exe");
    participant = new ChromeDriver(options1);
    participant.manage().window().maximize();
    
}

@AfterMethod
public void tearDown(ITestResult result) {
    
    if(result.getStatus() == ITestResult.FAILURE) {
        
    } else if(result.getStatus() == ITestResult.SKIP) {
        
    } else if(result.getStatus() == ITestResult.SUCCESS) {
        
        String methodName = result.getMethod().getMethodName();
        String logText = "<b>" + "TEST CASE: - "+methodName.toUpperCase()+" PASSED"+ "</b>"; 
        
        Markup m = MarkupHelper.createLabel(logText, ExtentColor.GREEN);
        test.pass(m);
    }
}
@Test(priority=1)
public void CreateRoomFrmBrowser() throws InterruptedException 
{
    test = extent.createTest("Create Room");
    admin.get("https://xxx.xxxxx.com/");
    test.info("Application started");
    admin.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    System.out.println("Navigated to XXXXxxxonline page");
    WebElement room_Name_field = admin.findElement(By.id(obj.getProperty("txt_room_name_field")));
    room_Name_field.clear();
    room_Name_field.sendKeys("TestDemo");
    admin.findElement(By.id(obj.getProperty("btn_Go"))).click();
    test.pass("Created the room successfully");
    Thread.sleep(3000); 
    String Copy_SharingLink = getElementText(admin, (By.xpath(obj.getProperty("txt_share_link"))));
    System.out.println("Copied link - " + Copy_SharingLink);
    
}

@Test(priority = 2)
public void JoinCallFrmParticipant() throws InterruptedException 
{
    test = extent.createTest("Copy Url");
    
participant.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
url = admin.getCurrentUrl();
System.out.println("Current room url : " + url);
participant.get(url);
Thread.sleep(3000);

}

I have created 2 webdriver instances in my code, as i have to join the call from 2 browsers(chrome,incognito). Other than this i have base page where i have written classes and a repository for locators. Previously it was working fine, dont know what happen suddenly when i try to run the code, it is throwing java.lang.NullPointerException . Please have a look and let me know where am doing wrong, Thanks in advance

Upvotes: 0

Views: 280

Answers (1)

Darshana
Darshana

Reputation: 621

action = new Actions(admin);

//some code

admin = new ChromeDriver(options);
admin.manage().window().maximize();

Before pass admin instance as a parameter of Actions(), admin has to initialize. At that place, It is throwing NPE.

admin = new ChromeDriver(options);
admin.manage().window().maximize();

action = new Actions(admin);

//some code

Upvotes: 1

Related Questions