vba 从 Excel Sheet 读取数据时得到 NullpointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24589443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:42:41  来源:igfitidea点击:

While reading data from Excel Sheet got NullpointerException

javaexcelvbaexcel-vbaselenium-webdriver

提问by Shantanu Nandan

I am trying to read data from a excel sheet but getting NullPointerException every time when reading data at the cell index =6. Put while(value != null) to avoid null values but still got the exception without any output. I am putting the screen shot of excel sheet from where i am trying to get data. enter image description here

我试图从 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)

回答by Eran

It's not enough to check that row.getCell(j).getStringCellValue() != null. You should check that row.getCell(j) != null.

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

In addition, your while loops makes no sense :

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

The first one will either do nothing or print value forever (since you are not changing value inside the loop).

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

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

The second one will either do nothing or increment j forever (since you are not changing value inside the loop).

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

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

I suggest you replace them with the following code :

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

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);
                }
            }
        }   
    }
}