Java Apache POI 在读取 CSV 转换的单元格数据时出现空指针异常

Java Null Pointer Exception for Apache POI in reading cell data for CSV conversion

我正在尝试为此 application:one 做两件事当 class 被调用时,它将 运行 从上到下和两个,我可以调用 public 方法 "xlxTOcsvConverter" 并提供三个需要的参数以供将来使用...也就是说,如果 class 是 运行,将输入 4 个文件,然后将输入 4 个文件书面。前三个工作得很好,但最后一个,带有评论的那个,似乎因给定错误而失败。现在我遇到的问题是,其中的数据由外部公司维护,我们只有读取权限,所以我所能做的就是要求他们维护一组单元格格式,但这可能不会坚持下去。对于失败的单元格,我注意到它被格式化为日期,但它是空的。所有其他空单元格都是通用格式;它实际上是它周围唯一格式化的单元格。我猜这就是它失败的原因。如果 cell_types?

,我是否还缺少其他一些内容
/*
* To change this license header, choose License Headers in Project     Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package datarefresh;

import java.io.FileInputStream;
import java.io.FileWriter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

/**
*
* @author null
*/
public class DataRefresh {

public static String EOL = System.getProperty("line.separator"); //Finding out OS specific EOL and setting it to a string named EOL

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
    String StrInfoXLS = "\\path\Dashboard.xls";
    String POSPwXLS = "\\path\POS Passwords.xls";
    String NetPwXLS = "\\path\Dashboard Data.xls";
    String SnLPwXLS = "\\path\AccessVia ID & Passcodes - Helpdesk.xls";
    String StrInfoCSV = "\\path\StoreInfoTst.csv";
    String POSPwCSV = "\\path\POS PasswordsTst.csv";
    String NetPwCSV = "\\path\Dashboard DataTst.csv";
    String SnLPwCSV = "\\path\AccessVia ID & Passcodes - HelpdeskTst.csv";       
    String StrInfoSht = "Store List";
    String POSPwSht = "Sheet1";
    String NetPwSht = "Network";
    String SnLPwSht = "S & L Store List w Passcode";
    xlxTOcsvConverter(StrInfoXLS,StrInfoSht,StrInfoCSV);
    xlxTOcsvConverter(POSPwXLS,POSPwSht,POSPwCSV);
    xlxTOcsvConverter(NetPwXLS,NetPwSht,NetPwCSV);
    xlxTOcsvConverter(SnLPwXLS,SnLPwSht,SnLPwCSV);   //THIS ONE IS NOT WORKING
}

public static void xlxTOcsvConverter(String inFile, String inSheet, String outFile) {
    DataRefresh GetIt = new DataRefresh();
    String data = "";
    try{
        FileWriter output = new FileWriter(outFile);
        Workbook wb = WorkbookFactory.create(new FileInputStream(inFile));
        Sheet sheet = wb.getSheet(inSheet);
        Row row = null;
        for(int i = 0; i<sheet.getLastRowNum(); i++){
            row = sheet.getRow(i);
            for(int j = 0; j<row.getLastCellNum(); j++){
                Cell cell = row.getCell(j);
                data = GetIt.getCellValue(cell);
                output.write(data + ";");
            }
            output.write(EOL);
        }
        output.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
}

private String getCellValue(Cell cell){
    String data = "";
    if(cell == null){
        return "";
    }
    if(cell.getCellType() == Cell.CELL_TYPE_BLANK){
        return "";
    }
    if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
        switch (cell.getCachedFormulaResultType()){
            case Cell.CELL_TYPE_NUMERIC:
                if(DateUtil.isCellDateFormatted(cell)){
                    data = String.valueOf(cell.getDateCellValue());
                    return data;
                }else{
                    double temp = cell.getNumericCellValue();
                    data = String.format("%.0f", temp);
                    return data;
                }
            case Cell.CELL_TYPE_STRING:
                data = cell.getStringCellValue();
                return data;
        }
    }
    if(cell.getCellType() == Cell.CELL_TYPE_STRING){
        data = cell.getStringCellValue();
        return data;
    }
    if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
        if(DateUtil.isCellDateFormatted(cell)){
            data = String.valueOf(cell.getDateCellValue());
            return data;
        }else{
            double temp = cell.getNumericCellValue();
            data = String.format("%.0f", temp);
            return data;
        }
    }
    return "";
}

}

这是它的堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:214)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:186)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:173)
    at jdk.nashorn.internal.objects.Global.checkObject(Global.java:1500)
    at jdk.nashorn.internal.objects.NativeError.printStackTrace(NativeError.java:187)
    at datarefresh.DataRefresh.xlxTOcsvConverter(DataRefresh.java:68)
    at datarefresh.DataRefresh.main(DataRefresh.java:45)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)

我将捕获更改为:

    catch(IOException | InvalidFormatException e){
        printStackTrace(e);
    }

现在堆栈跟踪 returns:

Exception in thread "main" java.lang.NullPointerException
    at datarefresh.DataRefresh.xlxTOcsvConverter(DataRefresh.java:60)
    at datarefresh.DataRefresh.main(DataRefresh.java:47)
Java Result: 1

如果第 0 行和第 sheet.getLastRowNum() 行之间完全是空行,则 rowrow = sheet.getRow(i) 之后将为 NULL。这是因为 XLS 文件中没有物理存储完全空的行。

这个你应该考虑。示例:

public static void xlxTOcsvConverter(String inFile, String inSheet, String outFile) {
    DataRefresh GetIt = new DataRefresh();
    String data = "";
    try {
        FileWriter output = new FileWriter(outFile);
        Workbook wb = WorkbookFactory.create(new FileInputStream(inFile));
        Sheet sheet = wb.getSheet(inSheet);

        Row row = null;
        for(int i = 0; i<=sheet.getLastRowNum(); i++) {
            row = sheet.getRow(i);
            if (row == null) {
                data = "empty row";
                output.write(data);
            } else {
                for(int j = 0; j<row.getLastCellNum(); j++) {
                    Cell cell = row.getCell(j);
                    data = GetIt.getCellValue(cell);
                    output.write(data + ((j<row.getLastCellNum()-1) ? ";" : ""));
                }
            }
            output.write(EOL);
        }


        output.close();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}