简体   繁体   English

Apache POI和Null指针…csv转换为xlsx

[英]Apache POI and Null Pointer… csv converted to xlsx

I am using Apache poi to parse through Excel 2010 files. 我正在使用Apache poi来解析Excel 2010文件。 I have a csv file that has the same content as one of my excel files. 我有一个csv文件,其内容与我的excel文件之一相同。 I opened my csv file in excel and saved it as an Excel workbook thinking that it should work in the program that I wrote but I keep getting null pointer exceptions. 我在excel中打开了csv文件,然后将其保存为Excel工作簿,以为它应该在我编写的程序中起作用,但是我不断收到空指针异常。 These happen on the lines of code that look up cells in the file when given a row number and a column number. 给定行号和列号时,这些操作发生在查找文件中单元格的代码行上。 I havent been able to find the cause for these null pointers and it works completely fine on my regular excel file. 我还无法找到这些空指针的起因,并且在我的常规excel文件中它完全可以正常工作。 Is it just that the file was previously a csv file? 只是该文件以前是一个csv文件吗?

I am using the XSSF Usermodel 我正在使用XSSF用户模型

This is where it fails 这是失败的地方

RPCManager rpc = new RPCManager(
    sheet.getRow(row).getCell(indexes.name).toString(), 
    sheet.getRow(row).getCell(indexes.status).toString(), 
    sheet.getRow(row).getCell(indexes.customer).toString(), 
    sheet.getRow(row).getCell(indexes.site).toString(), 
    sheet.getRow(row).getCell(indexes.suite).toString(), 
    sheet.getRow(row).getCell(indexes.pod).toString(), 
    sheet.getRow(row).getCell(indexes.amps).toString(), 
    sheet.getRow(row).getCell(indexes.pp1).toString(), 
 ** sheet.getRow(row).getCell(indexes.pp2).toString(), **
    sheet.getRow(row).getCell(indexes.pp3).toString(), 
    sheet.getRow(row).getCell(indexes.cab).toString(), 
    sheet.getRow(row).getCell(indexes.rpcNum).toString(), 
    sheet.getRow(row).getCell(indexes.rpcPan).toString(), 
    sheet.getRow(row).getCell(indexes.rpcBr).toString(), 
    row);

"indexes.whatever" is just the index of the column and row is the row “ indexes.whatever”只是列的索引,而row是行

This is a small part of the program that takes information in an excel file and writes an XML file. 这是程序的一小部分,该程序在excel文件中获取信息并编写XML文件。 The Excel file has 2500-3000 rows and spits out around 70 XML files. Excel文件具有2500-3000行,并吐出约70个XML文件。 The code above saves the needed information from the excel file to a class. 上面的代码将所需的信息从excel文件保存到一个类中。 This part of code is called around 2500 times. 这部分代码被调用了大约2500次。 I'm guessing it broke on its first time around. 我猜它是第一次破裂。 It only gives the null pointer exception and the location where it happened which is on the line with the asterisks above. 它仅给出空指针异常及其发生的位置(与上面的星号对齐)。

Sorry I am kind of new to programming so I am pretty clueless 抱歉,我是编程的新手,所以我一无所知

The null pointers occur because there is nothing in the worksheet at that row/column location. 之所以会出现空指针,是因为该行/列位置的工作表中没有任何内容。 You need to explicitly verify that there's a cell there before trying to fetch it. 您需要先明确验证那里是否有一个单元,然后再尝试获取它。

What you do if a cell is null depends totally on how RPCManager handles missing data. 如果单元格为空,该怎么RPCManager完全取决于RPCManager如何处理丢失的数据。 If it can cope with nulls then something like the following might work: 如果它可以处理空值,则可能会执行以下操作:

int[]    cellNums = {indexes.name, indexes.status, ... ,indexes.rpcBr};
String[] cellVals = new String[cellNums.length];
XSSFRow r = sheet.getRow(row);
for (int i=0; i<cellNums; i++)
{
    XSSFCell c = r.getCell(cellNums[i]);
    cellVals[i] = c == null ? null : c.toString();
}
RPCManager rpc = new RPCManager(
    cellVals[0],
    cellVals[1],
    ...
    cellVals[13],
    row);

Otherwise you'll have to decide what to do when there is missing data. 否则,您将不得不决定缺少数据时该怎么办。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM