【问题标题】:While reading data from Excel Sheet got NullpointerException从 Excel 表读取数据时出现 NullpointerException
【发布时间】:2023-03-23 13:37:02
【问题描述】:

我正在尝试从 Excel 工作表中读取数据,但每次在单元格索引 =6 处读取数据时都会出现 NullPointerException。放置 while(value != null) 以避免空值,但仍然得到没有任何输出的异常。 我正在从我试图获取数据的地方放置 excel 表的屏幕截图。

Code-
package com.selenium;

import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.IOException;

public class Exxcel {
  public static void main(String[] args) throws Exception,NullPointerException{
    //WebDriver driver= new FirefoxDriver();
    //WebElement wb;
    try{
    FileInputStream file= new FileInputStream("C:\\Documents and Settings\\OMEGA\\Desktop\\Test Planning And Documents\\Automation Data.xlsx");
    Workbook data=WorkbookFactory.create(file);
    Sheet sheet=data.getSheet("Sheet1");
    for(int i=1;i<=sheet.getLastRowNum();i++){
        Row row= sheet.getRow(i);
        int j=0;
        String value=row.getCell(j).getStringCellValue();
        while(value != null){ 
        System.out.println(value);
        }//while
        while(value == null){
            j++; 
         }  
    }//for

         /*while(j1==9){
             String value=row.getCell(j1).getStringCellValue();
             System.out.println(value);
             }//while2
             */
    }catch(NullPointerException n){n.printStackTrace();
        System.out.println("Null");
        }// catch
    }//main
 }//class


StackTrace-
Null
java.lang.NullPointerException
at com.selenium.Exxcel.main(Exxcel.java:22)

【问题讨论】:

    标签: java vba excel selenium-webdriver


    【解决方案1】:

    仅检查row.getCell(j).getStringCellValue() != null 是不够的。你应该检查row.getCell(j) != null

    此外,您的 while 循环毫无意义:

    第一个要么什么都不做,要么永远打印值(因为你没有在循环内改变值)。

        while(value != null) { 
            System.out.println(value);
        }//while
    

    第二个要么什么都不做,要么永远递增 j(因为你没有在循环内改变值)。

        while(value == null) {
            j++; 
        } 
    

    我建议你用以下代码替换它们:

    Row row = sheet.getRow(i);
    if (row != null) {
        for (int j = 0; j < row.getLastCellNum(); j++) {
            if (row.getCell(j) != null) {
                if (row.getCell(j).getCellType() == CELL_TYPE_STRING) {
                    String value=row.getCell(j).getStringCellValue();
                    if(value != null) { 
                        System.out.println(value);
                    }
                }
            }   
        }
    }
    

    【讨论】:

    • 代码是否按照您所说的进行了更改,但仍然出现没有任何值/输出的异常
    • @Shantanu 您在哪一行得到异常?也许您也应该验证该行!= null。
    • @EranSir 当我把 row != null....是否可以独立获取每个单元格的值。例如 String s1 = 单元格 1 的值,String s2= 第二个单元格的值
    • @Shantanu 在循环内创建数组没有任何好处,因为在前面的列中读取的值将被覆盖。此外,不建议在您的情况下使用数组,因为您事先不知道必须存储在此数组中的字符串数量。将数组初始化为具有 i 个元素是没有意义的,因为 i 只是当前行的索引。
    • @Shantanu 在开头(在你开始阅读行之前),做List&lt;String&gt; list = new ArrayList&lt;String&gt;();然后在每次从文件中读取新值时执行list.add(value)
    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多