简体   繁体   English

在 Selenium 中尝试读取 Excel 时出现空指针异常

[英]Nullpointer Exception While Using Trying to Read Excel in Selenium

Hi guys i Searched Every Where Solution For But Can't Find.大家好,我搜索了所有解决方案但找不到。 Why Am Getting Null Pointer Exception For This i Dunno.为什么我不知道为什么会出现空指针异常。 Please Sort Me This Out.请帮我解决这个问题。 It is Showing as Path is Only Wrong But i Specified it Correctly only.它显示为路径仅错误但我仅正确指定了它。

My Code :我的代码:

package UsingExcel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.sun.rowset.internal.Row;

public class Demo 
{

    public void ReadExcel(String filepath,String filename,String Sheetname) throws IOException
    {
        File file = new File(filepath); // line 21

        FileInputStream stream = new FileInputStream(file);

        Workbook Mybook = null;

        String FileExtensionnname = filename.substring(filename.indexOf("."));

        if(FileExtensionnname.equals(".xlsx"))
        {
            Mybook = new XSSFWorkbook(stream);
        }
        else if(FileExtensionnname.equals(".xls"))
        {
            Mybook = new HSSFWorkbook(stream);
        }

        Sheet filesheet = Mybook.getSheet(Sheetname);

        int rowcount = filesheet.getLastRowNum()-filesheet.getFirstRowNum();

        for(int i=0;i<rowcount+1;i++)
        {
            org.apache.poi.ss.usermodel.Row row =filesheet.getRow(i);

            for(int j=0;j<row.getLastCellNum();j++)
            {
                System.out.println(row.getCell(j).getStringCellValue()+ "||");
            }

            System.out.println();
        }
    }

    public static void main(String[] args) throws IOException
    {
        Demo excelfile =  new Demo();

        String filepath = System.getProperty("E:\\Mybook.xlsx");

        excelfile.ReadExcel(filepath, "Mybook.xlsx", "DemoExcel");
        }

}

My Error is :我的错误是:

Exception in thread "main" java.lang.NullPointerException
    at java.io.File.<init>(Unknown Source)
    at UsingExcel.Demo.ReadExcel(Demo.java:21)
    at UsingExcel.Demo.main(Demo.java:61)

Hope You Have Understood My Problem, Please Sort This out.希望你已经理解我的问题,请解决这个问题。 But When am Testing a Login Page Using Excel That No Problem Will Be Coming, Now i Try To Print on The Console it is Not Working.但是当我使用 Excel 测试登录页面时不会出现问题,现在我尝试在控制台上打印它不起作用。

Your filepath should just be你的文件路径应该只是

String filepath = "E:\\\\Mybook.xlsx" , don't use System.getProperty . String filepath = "E:\\\\Mybook.xlsx" ,不要使用System.getProperty

From docs :从文档:

Gets the system property indicated by the specified key获取指定键指示的系统属性

A null is being passed to your method ReadExcel(...) , because there is no System property defined as E:\\Mybook.xlsx正在将 null 传递给您的方法ReadExcel(...) ,因为没有定义为E:\\Mybook.xlsx 的系统属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM