英文:
Exception in thread "main" java.lang.NullPointerException error when calling a method to write a string into Excel sheet in JAVA
问题
Exception in thread "main" java.lang.NullPointerException error when calling a method to write a string into Excel sheet in JAVA. Error is as below
Exception in thread "main" java.lang.NullPointerException at ManualVolteCxCreation.ExcelDataReader.writeExcelTestResult(ExcelDataReader.java:51)
Calling writeExcelTestResult method from the main class and it is as below.
excelDataReader.writeExcelTestResult(startRowIndex, "There are already connections with");
Excel Data Reader class has the constructor in which initialize the excel file, excel workbook and the excel sheet and two methods to read excel data and write to the 7th column of the excel file. Excel Data Reader class is as below.
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelDataReader {
ConfigReader configReader = new ConfigReader();
File testFilePath;
FileInputStream fis;
XSSFWorkbook volteConnections;
XSSFSheet testDataFile;
XSSFRow rowTestData;
public ExcelDataReader() {
try {
testFilePath = new File(configReader.readPropertyValue("excel"));
fis = new FileInputStream(testFilePath);
// Finds the workbook instance for XLSX file
volteConnections = new XSSFWorkbook(fis);
// Return first sheet from the XLSX workbook
testDataFile = volteConnections.getSheetAt(0);
} catch (Exception e) {
e.printStackTrace();
}
}
public XSSFRow readExcelRowTestData(int rowIndex) {
rowTestData = testDataFile.getRow(rowIndex);
return rowTestData;
}
public void writeExcelTestResult(int rowIndex, String testResult) {
testDataFile.getRow(rowIndex).getCell(7).setCellValue(testResult);
}
}
英文:
Exception in thread "main" java.lang.NullPointerException error when calling a method to write a string into Excel sheet in JAVA. Error is as below
Exception in thread "main" java.lang.NullPointerException at ManualVolteCxCreation.ExcelDataReader.writeExcelTestResult(ExcelDataReader.java:51)
Calling writeExcelTestResult method from the main class and it is as below.
excelDataReader.writeExcelTestResult(startRowIndex, "There are already connections with");
Excel Data Reader class has the constructor in which initialize the excel file, excel workbook and the excel sheet and two methods to read excel data and write to the 7th column of the excel file. Excel Data Reader class is as below.
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelDataReader {
ConfigReader configReader = new ConfigReader();
File testFilePath;
FileInputStream fis;
XSSFWorkbook volteConnections;
XSSFSheet testDataFile;
XSSFRow rowTestData;
public ExcelDataReader() {
try {
testFilePath = new File(configReader.readPropertyValue("excel"));
fis = new FileInputStream(testFilePath);
// Finds the workbook instance for XLSX file
volteConnections = new XSSFWorkbook(fis);
// Return first sheet from the XLSX workbook
testDataFile = volteConnections.getSheetAt(0);
} catch (Exception e) {
e.printStackTrace();
}
}
public XSSFRow readExcelRowTestData(int rowIndex) {
rowTestData = testDataFile.getRow(rowIndex);
return rowTestData;
}
public void writeExcelTestResult(int rowIndex, String testResult) {
testDataFile.getRow(rowIndex).getCell(7).setCellValue(testResult);
}
}
I debug and tried to see where the problem, but found every variable was initialize as shown in below image.
So any suggestion how can I sort this issue?
答案1
得分: 0
你只需首先创建单元格,然后调用 setCellValue 方法。
为了保存文件,您还需要一个 FileOutputStream 对象。
因此,您可以按以下方式修改 writeExcelTestResult 方法。
public void writeExcelTestResult(int rowIndex, String testResult) {
if (cellTestData == null) {
cellTestData = testDataFile.getRow(rowIndex).createCell(7);
}
cellTestData.setCellValue(testResult);
try {
fis.close();
fos = new FileOutputStream(testFilePath);
volteConnections.write(fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
英文:
You just need to create the cell first and then call the setCellValue method.
In order to save the file you need FileOutputStream object as well.
So you can modify the writeExcelTestResult method as below.
public void writeExcelTestResult(int rowIndex, String testResult) {
if (cellTestData == null) {
cellTestData = testDataFile.getRow(rowIndex).createCell(7);
}
cellTestData.setCellValue(testResult);
try {
fis.close();
fos = new FileOutputStream(testFilePath);
volteConnections.write(fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论