线程“main”java.lang.NullPointerException(SearchCity.java:56)中的异常(SearchCity.java:104)
我已经编写了一个代码来读取excel文件以填充网页中的一些条目,我想从该网页获取一些数据并将其写入我的excel工作表,但我在代码的不同行中得到了NullPointerException,我认为这不是一个重复的问题,因此请给出一些答案 线程主java语言NullPointerException中出现异常 在SearchCity SearchCity tcSearchCity爪哇:56 在SearchCity SearchCity mainSearchCity爪哇:104线程“main”java.lang.NullPointerException(SearchCity.java:56)中的异常(SearchCity.java:104),java,selenium,webdriver,Java,Selenium,Webdriver,我已经编写了一个代码来读取excel文件以填充网页中的一些条目,我想从该网页获取一些数据并将其写入我的excel工作表,但我在代码的不同行中得到了NullPointerException,我认为这不是一个重复的问题,因此请给出一些答案 线程主java语言NullPointerException中出现异常 在SearchCity SearchCity tcSearchCity爪哇:56 在SearchCity SearchCity mainSearchCity爪哇:104 首先,在代码XSSFRo
首先,在代码XSSFRow row=sheet.getRow1中读取同一行两次;在XSSFRow row2=sheet.getRow1;中;。这不是必需的。然后,您需要检查该工作表是否为null,以及sheet.getRow1是否处于安全状态。接下来,读取行并检查单元格是否为空,如果不是,则设置值 这可能是你特定问题的解决方案
public class SearchCity {
public void tc() throws InterruptedException, IOException {
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("http://localhost/allmapview/");
Thread.sleep(3000);
ArrayList<String> data0 = readExcelData(0);
ArrayList<String> data1 = readExcelData(1);
for(int i=0; i<data0.size(); i++) {
driver.findElement(By.xpath(".//*[@id='from']")).sendKeys(data0.get(i));
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[@id='to']")).sendKeys(data1.get(i));
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[@id='calculate-route']/div[3]/div/div[2]/button")).click();
Thread.sleep(8000);
String strText = driver.findElement(By.id("google_dt")).getText();
String strText1 = driver.findElement(By.id("mmi_dt_2")).getText();
FileInputStream fis = new FileInputStream(new File("D:\\Screenshots\\Book1.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook (fis);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.getRow(1);
XSSFCell cell = row.getCell(2);
cell.setCellValue(strText);
XSSFRow row2 = sheet.getRow(1);
XSSFCell cell2 = row2.getCell(3);
cell2.setCellValue(strText1);
fis.close();
FileOutputStream fos =new FileOutputStream(new File("D:\\\\Screenshots\\\\Book1.xlsx"));
workbook.write(fos);
fos.close();
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new
File("D:\\Screenshots\\Screen" +i));
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='calculate-route']/div[3]/div/div[3]/input")).click();
Thread.sleep(3000);
}
}
public ArrayList<String> readExcelData(int colNo) throws IOException {
File src = new File("D:\\Screenshots\\Book1.xlsx");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet1 = wb.getSheetAt(0);
Iterator<Row> rowIterator = sheet1.iterator();
rowIterator.next();
ArrayList<String> list = new ArrayList<String>();
while(rowIterator.hasNext()) {
list.add(rowIterator.next().getCell(colNo).getStringCellValue());
}
System.out.println("List :::"+list);
return list;
}
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
SearchCity city = new SearchCity();
city.tc();
@VPK-its-city.tc;第56行更重要我猜问题就在这里FileOutputStream fos=new FileOutputStreamnew Field:\\\\\Screenshots\\\\Book1.xlsx;workbook.writefos;fos.close@shivam,您的异常基本上是在第56行抛出的。那一行的代码是什么?请通过调试检查您是否试图访问null object.cell.setCellValuestrText上的元素;这是第56行“建议我解决方案代码运行正常,但excel文件未使用strText和strText1Try更新”,以调试代码并检查行中是否有单元格。如果单元格为空,则无法更新该值。@顺便说一句,关于为什么不更新该文件,这将是另一个问题。如果您最初的NullPointerException问题已经解决,您可以将答案标记为已接受。您不必检查每个工作簿访问是否为null。@JeffC,这只是为了更安全起见,我在答案中说过。然而,你应该如何避免NPE?
if (sheet != null && sheet.getRow(1) != null) {
XSSFRow row = sheet.getRow(1);
if (row.getCell(2) != null) {
XSSFCell cell = row.getCell(2);
cell.setCellValue(strText);
}
if (row.getCell(3) != null) {
XSSFCell cell2 = row2.getCell(3);
cell2.setCellValue(strText1);
}
}