Exception in thread "main" java.lang.NullPointerException | Selenium Forum
M
Posted on 22/01/2017
Hi Team,

When I ran a selenium code , below mentioned error message is coming.For the code segment,please see the file attached

When I debug, exception error is throwing , inside function call....Please help..

[b:2i34qlq0]Exception in thread "main" java.lang.NullPointerException[/b:2i34qlq0]
at testcases.Rediff_2.isElementPresent(Rediff_2.java:52)
at testcases.Rediff_2.main(Rediff_2.java:40)

Thanks,
Anie Divakar

M
Replied on 22/01/2017

use this
[code:193tuoj0]
public static boolean isElementPresent(String xpathExpression){
List<WebElement> allElements ;
try{
allElements = driver.findElements(By.xpath(xpathExpression));
}catch(Exception e){
e.printstacktrace();
}

if (allElements.size()==0)
return false;
else
return true;
}
[/code:193tuoj0]


M
Replied on 23/01/2017

Now,when I ran the above given code , again a NullPointer exception is throwing....

kindly go through the screenshot..

I am using:
mozilla: 50.1.0
Eclipse: Luna 4.4.0
selenium: 3.0.1

Responsive image

M
Replied on 25/01/2017

zip and send me the project.


M
Replied on 06/02/2017

zipped file attached..


M
Replied on 06/02/2017

you're creating new instance of webdriver in main().

to fix it replace this code.

[code:2orsg3rz]
System.setProperty("webdriver.gecko.driver","D:\\geckodriver\\geckodriver_32bit\\geckodriver.exe");
driver=new FirefoxDriver();
driver.manage().window().maximize();[/code:2orsg3rz]