Java 尝试创建新单元格并将数据写入excel时出现空指针异常
我正在尝试将标题和价格写入excel文件。我正在创建列,但java game me在第48行出现错误“NULL POINTER EXCEPTION”,请帮助我主要原因是什么。但如果我在第48行sheet1.getRow(0.createCell(0.getStringCellValue('Naqash')写入;然后没有显示空指针错误Java 尝试创建新单元格并将数据写入excel时出现空指针异常,java,apache,selenium,Java,Apache,Selenium,我正在尝试将标题和价格写入excel文件。我正在创建列,但java game me在第48行出现错误“NULL POINTER EXCEPTION”,请帮助我主要原因是什么。但如果我在第48行sheet1.getRow(0.createCell(0.getStringCellValue('Naqash')写入;然后没有显示空指针错误 package codeclasses; import java.io.File; import java.io.FileInputStream; import
package codeclasses;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
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.chrome.ChromeOptions;
import org.testng.annotations.Test;
public class ReadExcel {
List<WebElement> title, prices;
@Test
public void test() throws IOException {
System.setProperty("webdriver.chrome.driver", "h:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://themeforest.net/search/education?referrer=homepage&utf8=%E2%9C%93");
title = driver.findElements(By.xpath("//h3[@class = 'product-list__heading']/a"));
prices = driver.findElements(By.xpath("//p[@class='product-list__price-desktop']"));
File src = new File("./file/Book1.xlsx");
FileInputStream file = new FileInputStream(src);
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet sheet1 = wb.getSheetAt(0);
for (int i = 0; i < 30; i++) {
int j = 0;
if(sheet1.getRow(i+1)==null){
(48) sheet1.getRow(i+2).createCell(j).setCellValue("Naqash");
sheet1.getRow(i+2).createCell(j+1).setCellValue("Zafar");
}
else{
System.out.println("Cant find the scene");
}
FileOutputStream fileout = new FileOutputStream(src);
wb.write(fileout);
}
}
}
包编解码器类;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.List;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.chrome.ChromeOptions;
导入org.testng.annotations.Test;
公共类ReadExcel{
清单名称、价格;
@试验
public void test()引发IOException{
System.setProperty(“webdriver.chrome.driver”,“h:\\chromedriver.exe”);
ChromeOptions选项=新的ChromeOptions();
options.addArguments(“禁用信息栏”);
options.addArguments(“--start maximized”);
WebDriver=新的ChromeDriver(选项);
驱动程序。获取(“https://themeforest.net/search/education?referrer=homepage&utf8=%E2%9C%93");
title=driver.findElements(By.xpath(“//h3[@class='product-list\uu heading']/a”);
prices=driver.findElements(By.xpath(//p[@class='product-list\uuuu price-desktop']);
文件src=新文件(“./File/Book1.xlsx”);
FileInputStream文件=新的FileInputStream(src);
XSSF工作簿wb=新XSSF工作簿(文件);
XSSFSheet sheet1=wb.getSheetAt(0);
对于(int i=0;i<30;i++){
int j=0;
如果(sheet1.getRow(i+1)==null){
(48)sheet1.getRow(i+2).createCell(j).setCellValue(“Naqash”);
sheet1.getRow(i+2).createCell(j+1).setCellValue(“Zafar”);
}
否则{
System.out.println(“找不到场景”);
}
FileOutputStream fileout=新的FileOutputStream(src);
wb.写入(文件输出);
}
}
}
在将数据插入行和单元格之前,请尝试创建行和单元格。例如:
int rowIndex = 0;
int columnIndex = 0;
Row row = sheet1.createRow(rowIndex);
Cell cell = row.createCell(columnIndex);
cell.setCellValue("Naqash");
如果您之前已经创建了行,那么可以通过
getRow(index)
获取行。问题是,您永远不会测试单元格是否为空
if (cell == null)
{
System.out.println("Cell is Empty in Column:" + cols);
}
else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
{
//code
}
一般来说,在处理Cell.getCellType()
函数时应该小心,因为空单元格可以是null
或Cell\u TYPE\u BLANK
我希望这会有帮助。可能是重复的谢谢你的回复,我得到了解决方案,继续帮助:)你成就了我的一天,谢谢@pomah:)@naqash,所以如果我的回答对你有帮助,你可以接受=)祝你好运!,是的,我做过投票,但我的声誉不允许反映投票结果。