带有当前线程上下文类加载器的NPE getResourceAsStream(文件)

时间:2015-06-18 18:44:16

标签: java spring-mvc nullpointerexception apache-poi

我正在尝试从资源文件夹中获取excel文件。这种方法很好用:

public JobOrderGenerator(List<ShopOrder> shopOrder) throws InvalidFormatException, IOException {

        inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("Shop-Order.xlsx");

        createJobOrder(shopOrder);

    }

哪个电话

void createJobOrder(List<ShopOrder> shopOrder) throws InvalidFormatException, IOException{


        for (ShopOrder shopOrder1 : shopOrder) {
            System.out.println("Inside createJobOrder "+shopOrder1.getPo_number());
            writeToSpecificCell(2, 1, sheetNumber, shopOrder1.getPo_number()); //Po Number
            writeToSpecificCell(7, 3, sheetNumber, shopOrder1.getPo_number()); //Part Number
            LocalDate date = shopOrder1.getPo_due_date();
            String dateToString = date.toString();
            writeToSpecificCell(1, 2, sheetNumber, dateToString); //Due_Date
            writeToSpecificCell(7, 5, sheetNumber, Integer.toString(shopOrder1.getPart_quantity())); //Quantity
            //writeToSpecificCell(1,2,sheetNumber, shopOrder.get); //Material
            writeToSpecificCell(8, 3, sheetNumber, shopOrder1.getPart_decription()); //Part Description
            //writeToSpecificCell(1,2,sheetNumber, shopOrder.getCustomer()); //Customer
            writeToSpecificCell(10, 1, sheetNumber, shopOrder1.getMachine_number()); //Machine

            sheetNumber++;

        }

    }

这是下面的罪魁祸首。正如您所看到的,我使用println语句来弄清楚发生了什么,我在控制台中看到的是:

Inside writeToSpecificCell before try statement 1 0 111

之后是java.lang.NullPointerException:

因此我的excel文件出现了问题。我只是无法弄清楚是什么。因为我检索它像:

inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("Shop-Order.xlsx");

它应该知道文件的位置。既然它在我的资源文件中而不在我的类路径上(所以它将在我的jar文件中)我需要以上述方式获取文件吗?

如果重要的是我正在使用Intellji Ultimate,仍在学习细节,我是一个沉重的日食用户。

class path

-----------更新1 -----------------

inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("/resources/Shop-Order.xlsx");

仍然得到同样的错误......

--------更新2 --------------------

try{
            ClassLoader classLoader = getClass().getClassLoader();

            inputStream = this.getClass().getResourceAsStream("/resources/Shop-Order.xlsx");

            if (inputStream == null){

                System.out.println("Inputstream is null....");
            }

只是为了确认我也做了:

inputStream = this.getClass().getResourceAsStream("/resources/Shop-Order .xlsx");

确认Shop-Order.xlsx中没有空格......

确认Inputstream为null,因为在控制台中它正在打印:

Inputstream is null...

来自我的println声明

抛出此null异常的方法就在这里......

 void writeToSpecificCell(int rowNumber, int cellNumber, int sheetNumber, String value) throws InvalidFormatException, IOException {

        System.out.println("inside writeToSpecificCell method before try statement");

        try {
            System.out.println("inside writeToSpecificCell method inside try statement");
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);

            XSSFSheet sheet = workbook.getSheetAt(sheetNumber);

            XSSFRow row = sheet.getRow(rowNumber);
            XSSFCell cell = row.createCell(cellNumber);

            if (cell == null) {
                cell = row.createCell(cellNumber);
            }
            cell.setCellType(Cell.CELL_TYPE_STRING);
            cell.setCellValue(value);
            System.out.println("inside writeToSpecificCell method after try statement");
            workbook.close();

        } catch (IOException e) {

            System.out.println("Error in writeToSpecificCell method " + e);

        }catch(NullPointerException ex){
        System.out.println("Null in writeToSpecificCell method");
    }
    }

3 个答案:

答案 0 :(得分:1)

在我看来,在文件扩展名之前你有一个空格。你能看出我是否正确吗? :P

答案 1 :(得分:1)

您也可以考虑使用getClass().getResourceAsStream("/Shop-Order.xlsx"),这样可以更好地获得适当的类加载器。

正如其他人所提到的那样,根据您的屏幕截图,文件(在磁盘上)也显示在.之前的空格。

另一个罪魁祸首可能是IDE的构建系统集成,有时刷新/重建可以解决这个问题。

答案 2 :(得分:0)

从类加载器的角度来看,此资源的正确名称为"/resources/Shop-Order. xlsx"