qq_36478826 2016-12-11 07:37 采纳率: 0%
浏览 2509

关于JAVA用jxl读取Excel出现空指针异常问题

``` public boolean ReadFromExcel(JFrame frame) {

    List<Student> list = new ArrayList<Student>();

    int count = 0 ;

    int result = 0 ;
    File file = null ;
    JFileChooser fileChooser = new JFileChooser();

    jxl.Workbook readwb = null;
    try {
        fileChooser.setApproveButtonText("确定");
        fileChooser.setDialogTitle("打开文件");
        result = fileChooser.showOpenDialog(frame);
        if(result == JFileChooser.APPROVE_OPTION){
            file = fileChooser.getSelectedFile() ;
        }else if(result == JFileChooser.CANCEL_OPTION){
            int result1 = JOptionPane.showConfirmDialog(null, "你未选择文件确定要退出吗?","提示", JOptionPane.INFORMATION_MESSAGE);
            if(result1 == 0){
                JOptionPane.showMessageDialog(null, "退出成功","提示", JOptionPane.INFORMATION_MESSAGE);
            }
        }else{
            JOptionPane.showMessageDialog(null, "操作有误!","警告", JOptionPane.ERROR_MESSAGE);
        }
        if(file != null){
            System.out.println(file.getPath());
            readwb = Workbook.getWorkbook(new FileInputStream(file));
            // Sheet的下标是从0开始
            Sheet readsheet = readwb.getSheet(0); // 获取第一张Sheet表
            int rsColumns = readsheet.getColumns(); // 获取Sheet表中所包含的总列数
            int rsRows = readsheet.getRows(); // 获取Sheet表中所包含的总行数
            System.out.println(rsColumns+" "+rsRows);
            for (int i = 1; i < rsRows; i++) {
                Student stu = new Student();
                for (int j = 0; j < rsColumns; j++) {
                    System.out.println(j+" "+i);
                    Cell cell = readsheet.getCell(j, i); // 获取指定单元格的对象引用
                    System.out.println(cell.getContents());
                    switch (j) {
                    case 0:
                        stu.setNumber(cell.getContents());
                        break;
                    case 1:
                        stu.setName(cell.getContents());
                        break;
                    case 2:
                //      System.out.println(Integer.parseInt(cell.getContents()));
                        stu.setAge(Integer.parseInt(cell.getContents()));
                        break;
                    case 3:
                        stu.setSex(cell.getContents());
                        break;
                    case 4:
                        stu.setProvince(cell.getContents());
                        break;
                    case 5:
                        stu.setMajor(cell.getContents());
                        break;
                    case 6:
                        stu.setPhoneNum(cell.getContents());;
                        break;
                    case 7:
                        stu.setBirthday(cell.getContents());
                        break;
                    case 8:
                        stu.setHabit(cell.getContents());
                        break;
                    default:
                        break;
                }
            }
            list.add(stu);
            System.out.println(stu);
            readwb.close();
        }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (BiffException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    for(int i = 0 ;i< list.size() ;i++){
    //  System.out.println("1111");
        Student stu1 = list.get(i) ;
        if(!checkStudent(stu1)){
            count++ ;
            try{
            //  System.out.println("1111");
                add(stu1) ;
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    JOptionPane.showMessageDialog(null, "成功读取"+count+"条数据到数据库中","提示", JOptionPane.INFORMATION_MESSAGE);
    return true ;
}
上面代码中,是通过文件选择器来选择文件
![这个是异常的图片](https://img-ask.csdn.net/upload/201612/11/1481441815_126992.png)
![这个是表的图片](https://img-ask.csdn.net/upload/201612/11/1481441826_895589.png)
  • 写回答

1条回答 默认 最新

  • 毕小宝 博客专家认证 2016-12-11 08:15
    关注
     首先,在用Java处理excel文件的时候,很容易存在某个单元格为null的情况,正常一个Excel文件,如果我们编辑一行数据时,第一个单元格完成后,直接
    跳过第二个单元格,编辑第三个单元格,那么用POI读取到的第二个单元格Cell对象就是null.的也就是说用户编辑区域范围内,未操作的单元格都有可能为null的。
    所以,我们的代码里面一定要对每个对象进行判空,行对象、单元格对象都需要判空,以避免空指针。
    
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?