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

null pointer from sheet.drawingPatriarch.getChildren()

我试图读取我之前在 excel sheet 中插入的图像及其在这段代码中的位置,它在我的机器上工作正常但是当我将代码迁移到另一台电脑时我得到sheet.getDrawingPatriarch.getChildren()中的空指针异常,我尝试用谷歌搜索这个问题,但我没有找到解决方案,有人愿意帮助我吗?下面是代码:

   /* 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() returns 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 文件复制到新机器中,代码有效,我猜问题将是两台机器之间的一些不同步的 jar 文件,谢谢@akhil_mittal 和@Gagravarr 给我的建议,非常感谢!