Java.lang.NullPointerException | Selenium Forum
M
Posted on 04/07/2016
Hello Sir,

Can you please tell me how can I resolve this problem?

Screenshots are attached with exception.

Thank you, Sir

M
Replied on 04/07/2016

String xpathExpression has no values.

put this on line 26

String part=part1+i+part2;
isElementPresent(part);


M
Replied on 04/07/2016

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.firefox.FirefoxDriver;

public class Rediff2 {
static WebElement driver = null;

public static void main(String[] args) {
//*[@id='popular_cat']/h3[1]/a
//*[@id='popular_cat']/h3[2]/a
//*[@id='popular_cat']/h3[3]/a

//*[@id='popular_cat']/h3[14]/a
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// driver.get("http://shopping.rediff.com/?sc_cid=inhome_icon");
driver.navigate().to("http://shopping.rediff.com/?sc_cid=inhome_icon");

String part1 = "//*[@id='popular_cat']/h3[";
String part2 = "]/a";
int i = 1;
String part=part1+i+part2;
while(isElementPresent(part)){
String text = driver.findElement(By.xpath(part1+i+part2)).getText();
System.out.println(text);
driver.findElement(By.xpath(part1+i+part2)).click();
System.out.println(driver.getTitle());
// driver.get("http://shopping.rediff.com/?sc_cid=inhome_icon");
driver.navigate().back();
i++;
}
}

public static boolean isElementPresent(String xpathExpression) {
List<WebElement> allElements = driver.findElements(By.xpath(xpathExpression));
if(allElements.size()==0)
return false;
else
return true;
}

}
--------------------------------------------------------------------------------------------------------------------------------------

I am sorry Sir, I changed it .Still giving me error

Exception in thread "main" java.lang.NullPointerException
at Rediff2.isElementPresent(Rediff2.java:39)
at Rediff2.main(Rediff2.java:27)


M
Replied on 04/07/2016

use this

package com.sample;

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.firefox.FirefoxDriver;

public class Rediff2 {
static WebDriver driver = null;

public static void main(String[] args) {
// *[@id='popular_cat']/h3[1]/a
// *[@id='popular_cat']/h3[2]/a
// *[@id='popular_cat']/h3[3]/a

// *[@id='popular_cat']/h3[14]/a
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// driver.get("http://shopping.rediff.com/?sc_cid=inhome_icon");
driver.navigate().to("http://shopping.rediff.com/?sc_cid=inhome_icon");

String part1 = "//*[@id='popular_cat']/h3[";
String part2 = "]/a";
int i = 1;
String part = part1 + i + part2;
while (isElementPresent(part)) {
String text = driver.findElement(By.xpath(part1 + i + part2))
.getText();
System.out.println(text);
driver.findElement(By.xpath(part1 + i + part2)).click();
System.out.println(driver.getTitle());
// driver.get("http://shopping.rediff.com/?sc_cid=inhome_icon");
driver.navigate().back();
i++;
}
}

public static boolean isElementPresent(String xpathExpression) {
List<WebElement> allElements = driver.findElements(By
.xpath(xpathExpression));
if (allElements.size() == 0)
return false;
else
return true;
}

}


M
Replied on 05/07/2016

Thank you Sir,

It did work. Responsive image Responsive image