NPE与当前线程上下文的类装载器的getResourceAsStream(文件) [英] NPE with Current Thread Context Class Loader getResourceAsStream(file)
问题描述
我想从我的资源文件夹的Excel文件。这种方法工作得很好:
公共JobOrderGenerator(列表< ShopOrder> shopOrder)抛出InvalidFormatException,IOException异常{ 的InputStream = Thread.currentThread()getContextClassLoader()的getResourceAsStream(铺Order.xlsx)。; createJobOrder(shopOrder); }
其中要求
无效createJobOrder(列表< ShopOrder> shopOrder)抛出InvalidFormatException,IOException异常{
对于(ShopOrder shopOrder1:shopOrder){
的System.out.println(内部createJobOrder+ shopOrder1.getPo_number());
writeToSpecificCell(2,1,工作表NUMBER,shopOrder1.getPo_number()); //订单号
writeToSpecificCell(7,3,工作表NUMBER,shopOrder1.getPo_number()); //零件号
日期LOCALDATE = shopOrder1.getPo_due_date();
串dateToString =与Date.toString();
writeToSpecificCell(1,2,工作表NUMBER,dateToString); //截止日期
writeToSpecificCell(7,5,工作表NUMBER,Integer.toString(shopOrder1.getPart_quantity())); //数量
// writeToSpecificCell(1,2,工作表NUMBER,shopOrder.get); //材料
writeToSpecificCell(8,3,工作表NUMBER,shopOrder1.getPart_decription()); //部分说明
// writeToSpecificCell(1,2,工作表NUMBER,shopOrder.getCustomer()); //顾客
writeToSpecificCell(10,1,工作表NUMBER,shopOrder1.getMachine_number()); //机 工作表NUMBER ++; } }
这是下面的罪魁祸首。正如你可以看到我使用println语句弄清楚发生了什么事情,以及我在控制台中看到的是:
里面writeToSpecificCell之前try语句1 0 111
之后,它是显示java.lang.NullPointerException:
因此,有一些与我的Excel文件回事。我只是无法弄清楚。自从我检索到的像:
的InputStream = Thread.currentThread()getContextClassLoader()的getResourceAsStream(铺Order.xlsx)。;
应当知道该文件的位置。既然是在我的资源文件,而不是我的类路径中(所以它会在我的JAR文件),我需要得到的文件按上述方法对不对?
如果它的事项我使用Intellji旗舰版,还在学习的来龙去脉,我是一个沉重的Eclipse用户。
-----------更新1 -----------------
的InputStream = Thread.currentThread()getContextClassLoader()的getResourceAsStream(/资源/铺Order.xlsx)。;
仍然得到同样的错误...
--------更新2 --------------------
{尝试
的ClassLoader类加载器=的getClass()getClassLoader()。 的InputStream = this.getClass()的getResourceAsStream(/资源/铺Order.xlsx); 如果(InputStream的== NULL){ 的System.out.println(InputStream为null ......);
}
只是为了确认我还做了:
的InputStream = this.getClass()的getResourceAsStream(/资源/车间订单的.xlsx);
只是为了确认有没有开网店Order.xlsx ...
空格据证实,InputStream的为空,因为在控制台它是印刷
InputStream为null ...
从我的println语句,
引发此空异常的方法是在这里...
无效writeToSpecificCell(INT ROWNUMBER,诠释cellNumber,诠释工作表NUMBER,字符串值)抛出InvalidFormatException,IOException异常{ 的System.out.println(writeToSpecificCell方法中前try语句); 尝试{
的System.out.println(try语句里面writeToSpecificCell方法内部);
XSSFWorkbook工作簿=新XSSFWorkbook(InputStream的); XSSFSheet表= workbook.getSheetAt(工作表NUMBER); XSSFRow行= sheet.getRow(ROWNUMBER);
XSSFCell细胞= row.createCell(cellNumber); 如果(细胞== NULL){
电池= row.createCell(cellNumber);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(值);
的System.out.println(writeToSpecificCell方法内部try语句之后);
workbook.close(); }赶上(IOException异常五){ 的System.out.println(+ E的writeToSpecificCell方法错误); }赶上(NullPointerException异常前){
的System.out.println(在writeToSpecificCell方法空);
}
}
您也可以考虑的getClass()。的getResourceAsStream(/铺Order.xlsx)
,这会给你得到适当的类加载器的一个更好的机会。
正如其他人提到它也出现在文件(在磁盘上)权利之前有一个空间。
,根据你的截图。
另一个原因可能是你的IDE的构建系统集成,一些次刷新/重建可以解决这个问题。
I am trying to get an excel file from my resource folder. This method works fine:
public JobOrderGenerator(List<ShopOrder> shopOrder) throws InvalidFormatException, IOException {
inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("Shop-Order.xlsx");
createJobOrder(shopOrder);
}
Which calls
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++;
}
}
This is the culprit below. As you can see I used println statements to figure out what was going on, and what I see in the console is:
Inside writeToSpecificCell before try statement 1 0 111
right after that is the java.lang.NullPointerException:
Therefore there is something going on with my excel file. I just cannot figure out what. Since I retrieved the it like:
inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("Shop-Order.xlsx");
It should know the location of the file. Since it is in my resource file and not on my class path (so it will be in my jar file) I need to get the file the above way right?
If it matters I am using Intellji Ultimate, still learning the ins and outs, I was a heavy eclipse user.
-----------Update 1-----------------
inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("/resources/Shop-Order.xlsx");
Still getting the same error...
--------Update 2--------------------
try{
ClassLoader classLoader = getClass().getClassLoader();
inputStream = this.getClass().getResourceAsStream("/resources/Shop-Order.xlsx");
if (inputStream == null){
System.out.println("Inputstream is null....");
}
Just to confirm I also did:
inputStream = this.getClass().getResourceAsStream("/resources/Shop-Order .xlsx");
Just to confirm there was not a space in Shop-Order.xlsx...
It is confirmed that the Inputstream is null, because in the console it is printing:
Inputstream is null...
From my println statement
The method throwing this null exception is here...
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");
}
}
You may also consider getClass().getResourceAsStream("/Shop-Order.xlsx")
, this will give you a better chance of getting the appropriate class loader.
As others have mentioned it also appears the file (on disk) has a space right before the .
, according to your screenshot.
Another culprit may be the build system integration of your IDE, some times refreshing / rebuilding can fix this.
这篇关于NPE与当前线程上下文的类装载器的getResourceAsStream(文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!