Java Lang NullPointerException in Selenium

Java Lang NullPointerException in Selenium,While running the selenium test scripts you’ll face some challenges with the test scripts,in that challenges this is the main important exception,not able to understand why it is displaying and most of the time you’ll think that it is raised because of element is not identifying by the Selenium,that is not the proper answer.
Java Lang NullPointerException in Selenium

Java Lang NullPointerException in Selenium

Trending Posts:

Java Lang NullPointerException in Selenium

When coming to Java Lang NullPointerException in Selenium,when passing single webdriver instance to multiple classes,you’ll face this type of problems.Please read below instructions  to resolve the same issue in Selenium.
SelDriver Class:

 

package com.sel.Selenium.Practise;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeSuite;

public class SelDriver {
public WebDriver driver;
String baseurl = “http://www.google.co.in”;

@BeforeSuite
public void openBrowser(){

ALSO READ  Sikuli Guide For Beginners – Integrate Sikuli With Selenium Webdriver

driver = new FirefoxDriver();
//driver = new HtmlUnitDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);

}
}

GoogleSite Class:

 

package com.sel.Selenium.Practise;
import org.testng.Assert;
import org.testng.annotations.Test; 
public class GoogleSite extends SelDriver {

@Test
public void seleniumElements() throws InterruptedException{

driver.get(baseurl);
System.out.println(“Open Google site”);

ALSO READ  How to get the number of Elements in a list in Python

String Title=driver.getTitle();
Assert.assertEquals(Title, “Google”);

try {
Assert.assertEquals(driver.getTitle(), “Google’s”);
} catch (Error e) {
System.out.println(e.getCause());
}
}
}

Extending the class from SelDriver main class to use WebDriver instance,But when running the above class,you’ll face same issue i.e Java.Lang.NullPointerException for some methods.In order to resolve this issue,you need to add static for WebDriver declaration as below.

 

package com.sel.Selenium.Practise;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeSuite;

public class SelDriver {

public static WebDriver driver;
String baseurl = “http://www.google.co.in”;

@BeforeSuite
public void openBrowser(){

ALSO READ  How to Add Screenshots to TestNG Report

driver = new FirefoxDriver();
//driver = new HtmlUnitDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);

}
}

Before Static Method Test Results:

After Changes – Test Results

[TestNG] Running:
  E:QTPTutorialsSelenium_WorkspaceSelenium_Tutorialstestng.xml
Open Google site
null
driver=FirefoxDriver: firefox on WINDOWS (f2f86423-9806-4b4a-ae92-7504e3db59be)
Enter Search Keyword
Clear Enter Keyword
clear works
===============================================
GoogleTestSuite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
TestNG Test Results
Please provide your valuable comments on this solution in case it is resolved your java.lang.nullpointer exception in Selenium.

Rajesh K

Leave a Reply

Your email address will not be published. Required fields are marked *