空指针异常的Apache

空指针异常的Apache

本文介绍了空指针异常的Apache POI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜,我们一直在阅读XLS和使用Apache POI ING我们的Java程序XLSX文件,这个问题我们正在空指针异常有两个原因。第一是1,我们已经解决了这个空白单元格,另一种当我们在选择某一列没有任何记录。

hi we've been reading xls and xlsx file using apache poi ing our java program, the problem is we are getting null pointer exception with two reasons.. the first 1 is the blank cell which we already solved and the other one is when we are choosing a certain column that doesn't have any record..

我们的节目要求为EXCEL文件的路径,然后将文件和你想读..这里的表的具体列数的具体表编号为code读取xls文件

our program ask for the path of the excel file then the specific sheet number of the file and the specific column number of the sheet you want to read.. here is the code for reading xls file

public void readXLSFile()throws IOException{
    InputStream ExcelFileToRead = new FileInputStream(path);
    HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);


    HSSFSheet sheet=wb.getSheetAt(sheetname);
    HSSFRow row;
    HSSFCell cell;

    Iterator rows = sheet.rowIterator();

            list1.clear();

    while (rows.hasNext())
    {
                   headers.clear();
        row=(HSSFRow) rows.next();

                // Iterator cells = row.cellIterator();

                    headers.add("contents");


            cnt = cnt+1;

            cell = row.getCell(cols);
            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
            {
                //System.out.println(cell.getStringCellValue()+"(string)");
                list.add(cell.getStringCellValue());
                                    d.add(cell.getStringCellValue());
                                    list1.add(new KeyValuePair(cell.getStringCellValue(),""));
            }
            else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
            {
                //System.out.println(cell.getNumericCellValue()+"(numeric)");
                double num = cell.getNumericCellValue();
                String num2 = String.valueOf(num);
                list.add(num2);
                                     d.add(num2);
                                     list1.add(new KeyValuePair(num2,""));

            }
            else if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN)
            {
                //System.out.println(cell.getBooleanCellValue()+"(boolean)");
                String bool = String.valueOf(cell.getBooleanCellValue());
                list.add(bool);
                                     d.add(bool);
                                     list1.add(new KeyValuePair(bool,""));
            }
            else
            {
                //U Can Handel Boolean, Formula, Errors
            }


        //System.out.println();
    }
        arrey = list.toArray(new String[list.size()]);
                    data.add(d);
                   // System.out.println(data);


                     model = new DefaultTableModel();
                     table_1.setModel(model);


                   table_1.setModel(model);
                      model.setColumnIdentifiers(new String[] {"row","contents"});

                     for (KeyValuePair p : list1){


                       int nor=table_1.getRowCount();

                       int n2 = nor +1;
                        n1 = Integer.toString(n2);
                     //  model.addColumn(new String [] {n1});


                       model.addRow(new String[] {n1,p.key, p.value});


                     }
                  //   model.addColumn(new String[] {n1});

}

变量SHEETNAME是Excel文件的页号

the variable sheetname is for the excel file's sheet number

HSSFSheet sheet=wb.getSheetAt(sheetname);

和变量COLS是针对特定列你想读

and the variable cols is for the specific column you want to read

cell = row.getCell(cols);

我们可以读取每个板的第一列,也是第二片的第二列,但是当我编辑我的测试文件的程序现在只能读取每片的第一列..误差空指针异常..希望你能帮助在此先感谢

we can read the first column of every sheet and also the second column of the second sheet but when i edited my test file the program now can only read the first column of every sheet.. the error is null pointer exception..wish you could help thanks in advance

推荐答案

雷耶斯,

问题是,你永远测试,如果单元格是空!

The issue is that you never test if the cell is null!

if (cell == null)
{
   System.out.println("Cell is Empty in Column:" + cols);

} else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
{
   //code
}

作为一般的事情,你应该小心,同时处理 Cell.getCellType()函数,因为空单元格可以是或者是一个 CELL_TYPE_BLANK

As a general matter, you should be careful while handling Cell.getCellType() function, since an empty cell could be either null or be a CELL_TYPE_BLANK.

我希望它帮助。

这篇关于空指针异常的Apache POI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:42