NullPointerException当尝试将结果写入excel时 [英] NullPointerException when try to write result to excel
问题描述
我收到有关第41行的NullPointerException的错误消息。我知道它返回null,但这个值是下列验证所必需的。一般的任务是将结果写入excel文件。
public class ExcelUtils {
public static void main(String [] args)
{
//空白工作簿
XSSFWorkbook工作簿=新XSSFWorkbook();
//创建一个空白页
XSSFSheet sheet = workbook.createSheet(Employee Data);
String Result =TEST TEST TEST;
int RowNum = 2;
int ColNum = 3;
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow行;
尝试
{
Row = ExcelWSheet.getRow(RowNum);
Cell = Row.getCell(ColNum,org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if(Cell == null){
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
// ------------------- --------------------------------
//尝试
// {
//在文件系统中写入工作簿
FileOutputStream out = new FileOutputStream(new File(howtodoinjava_demo.xlsx));
workbook.write(out);
out.close();
System.out.println(howtodoinjava_demo.xlsx在磁盘上成功写入);
}
catch(异常e)
{
e.printStackTrace();
}
}
}
将Result字符串从测试方法传递给writeexcel方法。您不需要创建一个对象,因为方法是静态的
writeexcel(pass,2,3);
这将调用writeexcel方法,并将作为参数传递的字符串ie)pass
public static void writeexcel(String Result,int RowNum,int ColNum)
{
//空白工作簿
XSSFWorkbook工作簿=新的XSSFWorkbook();
//创建一个空白页
XSSFSheet sheet = workbook.createSheet(Employee Data);
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow行;
try
{
Row = ExcelWSheet.createRow(RowNum);
Cell = Row.getCell(ColNum,org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if(Cell == null){
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
FileOutputStream out = new FileOutputStream(new File(howtodoinjava_demo.xlsx));
workbook.write(out);
out.close();
System.out.println(howtodoinjava_demo.xlsx在磁盘上成功写入);
}
catch(异常e)
{
e.printStackTrace();
}
}
编辑
public class Tests {
static XSSFWorkbook workbook = new XSSFWorkbook();
static XSSFSheet sheet = workbook.createSheet(Employee Data);
public static void main(String [] args){
int j = 3; (int i = 2; i< = 5; i ++)
{
String result =Test+ i;
writeexcel(result,i,j);
}
writetoexcel(); //写入单元格(2,3),(3,3),(4,3)
}
public static void writeexcel(String Result,int RowNum,int ColNum)
{
//创建一个空白页
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow行;
try
{
Row = ExcelWSheet.createRow(RowNum);
Cell = Row.getCell(ColNum,org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if(Cell == null){
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
}
catch(异常e)
{
e.printStackTrace();
}
}
public static void writetoexcel(){
try {
FileOutputStream out = new FileOutputStream(new File(howtodoinjava_demo .XLSX));
workbook.write(out);
out.close();
System.out.println(howtodoinjava_demo.xlsx在磁盘上成功写入);
} catch(Exception e)
{
e.printStackTrace();
}
}
}
希望这个帮助你...如果您需要任何帮助,请回复
Can someone help with this code?
I receive an error message about NullPointerException at 41 row. I know that it returns null but this value is required for following verification. The general task is to write result to excel file.
public class ExcelUtils {
public static void main(String[] args)
{
//Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create a blank sheet
XSSFSheet sheet = workbook.createSheet("Employee Data");
String Result = "TEST TEST TEST";
int RowNum = 2;
int ColNum = 3;
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow Row;
try
{
Row = ExcelWSheet.getRow(RowNum);
Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
//---------------------------------------------------
//try
// {
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx"));
workbook.write(out);
out.close();
System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
pass the Result string from your test method to the method writeexcel.You don't need to create an object as the method is static
writeexcel("pass",2,3);
This will call the writeexcel method and will write the string passed as an argument ie) "pass" in the respective cell
Method :
public static void writeexcel(String Result,int RowNum ,int ColNum)
{
//Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create a blank sheet
XSSFSheet sheet = workbook.createSheet("Employee Data");
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow Row;
try
{
Row = ExcelWSheet.createRow(RowNum);
Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx"));
workbook.write(out);
out.close();
System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
}
catch (Exception e)
{
e.printStackTrace();
}
}
EDIT
public class Tests {
static XSSFWorkbook workbook = new XSSFWorkbook();
static XSSFSheet sheet = workbook.createSheet("Employee Data");
public static void main(String[] args) {
int j=3;
for(int i=2;i<=5;i++) {
String result = "Test "+i;
writeexcel(result, i, j);
}
writetoexcel();//write to cell(2,3),(3,3),(4,3)
}
public static void writeexcel(String Result,int RowNum ,int ColNum)
{
//Create a blank sheet
XSSFSheet ExcelWSheet = sheet;
XSSFCell Cell;
XSSFRow Row;
try
{
Row = ExcelWSheet.createRow(RowNum);
Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void writetoexcel(){
try{
FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx"));
workbook.write(out);
out.close();
System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
}catch (Exception e)
{
e.printStackTrace();
}
}
}
Hope this helps you...Kindly get back if you need any further help
这篇关于NullPointerException当尝试将结果写入excel时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!