来自sheet.drawingPatriarch.getChildren()的空指针

我试图读取图像我插入之前在Excel表中与此代码的位置,它在我的机器上工作正常,但是当我将代码迁移到另一台PC我得到空指针exception在sheet.getDrawingPatriarch.getChildren() ,我试图Googlesearch问题,但我没有find解决办法,任何人都想帮助我? 以下是代码:

/* loop the sheet */ for (int i = 0; i < sheetNumbers; i++) { sheet = wb.getSheetAt(i); /* create map to store id map with picture */ Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); /* determine to use 2003's excel get pic method or 2007++ get picture method */ if (fileExt.equals("xls")) { if(((HSSFSheet) sheet).getDrawingEscherAggregate() != null) sheetIndexPicMap = getSheetPictrues03(i, (HSSFSheet) sheet, (HSSFWorkbook) wb); } /* store the picture and id map into a list */ sheetList.add(sheetIndexPicMap); } printImg(sheetList); } public static Map<String, PictureData> getSheetPictrues03(int sheetNum, HSSFSheet sheet, HSSFWorkbook workbook) { Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); List<HSSFPictureData> pictures = workbook.getAllPictures(); if (pictures.size() != 0) { for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor(); if (shape instanceof HSSFPicture) { HSSFPicture pic = (HSSFPicture) shape; int pictureIndex = pic.getPictureIndex() - 1; HSSFPictureData picData = pictures.get(pictureIndex); HSSFRow row = sheet.getRow(anchor.getRow1()); HSSFCell cell = row.getCell(0); String picIndex = "ID"+String.valueOf(cell); sheetIndexPicMap.put(picIndex, picData); } } return sheetIndexPicMap; } else { return null; } } public static void printImg(List<Map<String, PictureData>> sheetList) throws IOException { for (Map<String, PictureData> map : sheetList) { Object key[] = map.keySet().toArray(); for (int i = 0; i < map.size(); i++) { /*get picture data*/ PictureData pic = map.get(key[i]); /*get row id where the picture reside*/ String picName = key[i].toString(); /*get file extension of the pictur*/ String ext = pic.suggestFileExtension(); byte[] data = pic.getData(); FileOutputStream out = new FileOutputStream("pic" + picName + "." + ext); out.write(data); out.close(); } } } 

在我看来,任何工作sheetnullsheet.getDrawingPatriarch()返回null 。 请把代码写成:

 if (pictures.size() != 0) { if(sheet!=null && sheet.getDrawingPatriarch()!=null && sheet.getDrawingPatriarch().getChildren()!=null) { for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor(); if (shape instanceof HSSFPicture) { HSSFPicture pic = (HSSFPicture) shape; int pictureIndex = pic.getPictureIndex() - 1; HSSFPictureData picData = pictures.get(pictureIndex); HSSFRow row = sheet.getRow(anchor.getRow1()); HSSFCell cell = row.getCell(0); String picIndex = "ID"+String.valueOf(cell); sheetIndexPicMap.put(picIndex, picData); } } } return sheetIndexPicMap; } else { return null; } 

问题解决了,我复制我的整个项目,而不是java文件到新的机器,代码的作品,我猜这个问题将是2机器之间的一些unsynchronzie jar文件,感谢@akhil_mittal和@Gagravarr给我的build议,谢谢你非常!