I am trying to get some data from the internet and then save it in a txt file but i am getting a Nullpointer Exception which reads: INFO: Detected dialect: W3C Exception in thread "main" java.lang.NullPointerException at mypackage.testClass.main(testClass.java:31). So, its actually directing me to this line: byte[] contentInBytes = aaa.getBytes();
Here is the code:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.*;
public class testClass {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.blablawebsite.com/path");
List<WebElement> urls = driver.findElements(By.xpath("//li[contains(@text(),'text')]"));
int i;
try {
File file = new File("/path/to/file/text.txt");
FileOutputStream fos = new FileOutputStream(file);
if(!file.exists())
file.createNewFile();
for(i=0; i <urls.size(); i++) {
String aaa = urls.get(i).getAttribute("data-asin");
byte[] contentInBytes = aaa.getBytes();
fos.write(contentInBytes);
fos.flush();
fos.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
This is not a specific solution but an advice to find the answer.
In order to find out when and why you are receiving that NullPointerException, you could temporarily print aaa
.
System.out.println("i = " + i + " -> aaa = " + aaa);
Add that line after aaa
initialization. Then you can apply the required logic to handle that scenario.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.