java.lang.NullPointerException尝试写入excel文件时:
我得到了一个java.lang.NullPointerException,通常我找不到罪魁祸首,但这次我非常困惑 首先,我确认我的JPQL是这样工作的:java.lang.NullPointerException尝试写入excel文件时:,java,spring-mvc,Java,Spring Mvc,我得到了一个java.lang.NullPointerException,通常我找不到罪魁祸首,但这次我非常困惑 首先,我确认我的JPQL是这样工作的: List<ShopOrder> shopOrders = poRepository.getShopOrder(id); for (ShopOrder<?> order : shopOrders) { System.out.println(order.toS
List<ShopOrder> shopOrders = poRepository.getShopOrder(id);
for (ShopOrder<?> order : shopOrders) {
System.out.println(order.toString());
然后我决定我需要一个更具体的错误。以下是上面代码段的完整方法:
@RequestMapping(value = "/generateShopOrder/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public void generate(@PathVariable Long id) throws URISyntaxException {
System.out.println("po id to generate = " + id);
List<ShopOrder> shopOrders = poRepository.getShopOrder(id);
for (ShopOrder<?> order : shopOrders) {
System.out.println(order.toString());
try {
jobOrderGenerator = new JobOrderGenerator(shopOrders);
System.out.println("Printing inside try statement: PO number " + order.getPo_number() + "\nPrinting part id " + order.getPart_id());
} catch (InterruptedException exception) {
System.out.println("Something is null " + exception);
exception.printStackTrace();
} catch (IOException ex) {
System.out.print("Was not able to create job orders,IOexception " + ex);
} catch (InvalidFormatException e) {
System.out.print("Invalid file format: " + e);
}
}
---------更新2------------
这是执行错误的语句:
if (env.acceptsProfiles(Constants.SPRING_PROFILE_DEVELOPMENT)) {
log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(), e.getCause(), e);
-----------更新3------------
我这样做是为了检查环境是否为null,并且在控制台中没有看到任何内容
@AfterThrowing(pointcut = "loggingPointcut()", throwing = "e")
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
if (this.env==null){
log.error("Environment is null");
}
-------------更新4------------
这就是返回java.lang.NullPointerException的内容,它甚至从未到达:
file = new File(classLoader.getResource("Shop-Order.xlsx").getFile());
这就是为什么我不能检查文件是否为空。还在研究原因。一旦我解决了这个问题,我可能需要更改这篇文章的标题
----------更新5--------------
我需要它,因为它在我的资源文件中,而资源文件将在我的jar文件中。我将写入文件(文件中的文件)
现在我必须弄清楚如何将其作为文件传递给ApachePOI
XSSFWorkbook workbook = new XSSFWorkbook(file);
它需要一个文件,而不是输入流
----------更新6---------------
在我完成更新6之后,我能够找出失败的地方。下面的方法。在控制台中,它打印:
Inside writeToSpecificCell before try statement 2 0 123
及
无法确定,因为源代码未完全显示,但
文件
为空似乎符合逻辑。它是构造函数中第一个输出之前引用的唯一对象。
试试这个
通常,为了解决这些问题,您只需要查看堆栈跟踪,它会立即告诉您问题所在。如@AndreasAumayr建议的那样,如果这不起作用,请使用调试器并中断异常(图标看起来像“J!”)或堆栈跟踪中提到的行。是否检查了日志记录方面?“[ERROR]com.htd.aop.logging.LoggingAspect-Exception in com.htd.web.rest.PoResource.generate(),cause=null”并没有说“Something is null”,但在ex.getCause()==null的地方引发了一个异常。@AndreasAumayr刚刚更新了代码。现在再仔细看一看。@AndreasAumayr的“@Afterhrowing”正在捕获null异常,但它没有告诉我它在哪里,所以我仍然想知道什么是null。可能变量env在执行时为null。您能验证注入是否按预期工作吗?@AndreasAumayr请参阅更新3
@AfterThrowing(pointcut = "loggingPointcut()", throwing = "e")
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
if (this.env==null){
log.error("Environment is null");
}
ClassLoader classLoader = getClass().getClassLoader();
file = new File(classLoader.getResource("Shop-Order.xlsx").getFile());
inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("Shop-Order.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(file);
Inside writeToSpecificCell before try statement 2 0 123
Inside writeToSpecificCell at the beginning of try statement
void writeToSpecificCell(int rowNumber, int cellNumber, int sheetNumber, String value) throws InvalidFormatException, IOException {
System.out.println("Inside writeToSpecificCell before try statement "+cellNumber+" "+sheetNumber+" "+value);
try {
System.out.println("Inside writeToSpecificCell at the beginnning of 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 at the end of try statement");
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (file == null) {
System.out.println("file is null");
} else if (file.exists() && file.isDirectory()){
System.out.println("Was was found");
} else {
System.out.println("File was NOT found");
}