Null pointer exception during verif of isElementPresent | Selenium Forum
M
Posted on 01/11/2016
I am getting this null pointer exception during verification of iselementpresent
Help appericiated
===========================================Code================

package seleniumfirstpro;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Exercise2SL {
static WebDriver c1 =null;


public static void main(String[] args) {

//WebDriver c1 = new FirefoxDriver();
System.setProperty("webdriver.chrome.driver","D:\\java learning\\selnium2.53.1\\chromedriver.exe");
WebDriver c1 = new ChromeDriver();
c1.manage().window().maximize();
c1.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
c1.get("http://www.americangolf.co.uk/golf-clubs/fairway-woods");

//To read Sum of all Products
String S1;
S1=c1.findElement(By.xpath("//*[@id='primary']/div[2]/div[1]/div[1]/span")).getText();
Integer Sum = Integer.parseInt(S1),i=1,j=2;
System.out.println("Sum = " +" "+Sum);

//=========================================
//Logic to Every link will have a number in front of it;Find sum of all numbers

String Part1T1 = "//*[@id='secondary']/div[1]/div[3]/div/ul/li[";
String Part1T2 ="]/a/span[1]";
String Part1T3 = "]/a/span[2]";

System.out.println(c1.findElements(By.xpath(Part1T1+i+Part1T2)).size());
System.out.println(isElementPresent(Part1T1+i+Part1T2));
/*while(isElementPresent(Part1T1+i+Part1T2)){
String text=c1.findElement(By.xpath(Part1T1+i+Part1T2)).getText();
System.out.println(text);
i++;


}*/




}


public static boolean isElementPresent(String xpathExpression){
System.out.println(c1.findElements(By.xpath(xpathExpression)).size());
int o = c1.findElements(By.xpath(xpathExpression)).size();
if(o==0)
return false;
else
return true;
}
}

========================OutPut=====================
Starting ChromeDriver 2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf) on port 9614
Only local connections are allowed.
Sum = 56
1
Exception in thread "main" java.lang.NullPointerException
at seleniumfirstpro.Exercise2SL.isElementPresent(Exercise2SL.java:55)
at seleniumfirstpro.Exercise2SL.main(Exercise2SL.java:39)

M
Replied on 01/11/2016

delete the red line

public static boolean isElementPresent(String xpathExpression){
[color=#FF0000:1tw4cnbx]System.out.println(c1.findElements(By.xpath(xpathExpression)).size());
[/color:1tw4cnbx]int o = c1.findElements(By.xpath(xpathExpression)).size();
if(o==0)
return false;
else
return true;
}
}


M
Replied on 01/11/2016

[quote="qtpselenium.supp0rt@gmail.com":1j71bg67]delete the red line

public static boolean isElementPresent(String xpathExpression){
System.out.println(c1.findElements(By.xpath(xpathExpression)).size());
int o = c1.findElements(By.xpath(xpathExpression)).size();
if(o==0)
return false;
else
return true;
}
}[/quote:1j71bg67]


It still fails with error on line: int o = c1.findElements(By.xpath(xpathExpression)).size();
When debug for some reason c1 though declared globally static ... it is pointing to null value..


M
Replied on 01/11/2016

Issue resolved: The problem was i had declared Webelement Twice...