Exception in thread "main" java.lang.NullPointerException error when calling a method to write a string into Excel sheet in JAVA

huangapple go评论88阅读模式
英文:

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.

Exception in thread "main" java.lang.NullPointerException error when calling a method to write a string into Excel sheet in JAVA

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();
		}    	  
    	
   }

huangapple
  • 本文由 发表于 2020年5月3日 23:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61577339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定