-2

I am trying to write title and price into excel file. I am creating columns but java game me error 'NULL POINTER EXCEPTION' at line number 48, Please help me what is main reason.But if i write at line no 48 ,sheet1.getRow(0).createCell(0).getStringCellValue('Naqash');Then no Null pointer error is showing.

package codeclasses;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.Test;

public class ReadExcel {

    List<WebElement> title, prices;

    @Test

    public void test() throws IOException {

        System.setProperty("webdriver.chrome.driver", "h:\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("disable-infobars");
        options.addArguments("--start-maximized");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://themeforest.net/search/education?referrer=homepage&utf8=%E2%9C%93");

        title = driver.findElements(By.xpath("//h3[@class = 'product-list__heading']/a"));

        prices = driver.findElements(By.xpath("//p[@class='product-list__price-desktop']"));


        File src = new File("./file/Book1.xlsx");
        FileInputStream file = new FileInputStream(src);

        XSSFWorkbook wb = new XSSFWorkbook(file);
        XSSFSheet sheet1 = wb.getSheetAt(0);

        for (int i = 0; i < 30; i++) {
            int j = 0;

            if(sheet1.getRow(i+1)==null){
(48)        sheet1.getRow(i+2).createCell(j).setCellValue("Naqash");
            sheet1.getRow(i+2).createCell(j+1).setCellValue("Zafar");
            }
            else{

                System.out.println("Cant find the scene");
            }
            FileOutputStream fileout = new FileOutputStream(src);
            wb.write(fileout);

        }

    }

}
jmargolisvt
  • 5,722
  • 4
  • 29
  • 46
naqash
  • 63
  • 2
  • 6
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – JFPicard Sep 18 '17 at 14:10

2 Answers2

0

Try to create the row and cell before you insert data into it. For example:

int rowIndex = 0;
int columnIndex = 0;
Row row = sheet1.createRow(rowIndex);
Cell cell = row.createCell(columnIndex);
cell.setCellValue("Naqash");

You can get row by getRow(index) if you have already created it before

Roman Danilov
  • 351
  • 2
  • 14
0

The issue is that you never test if the cell is null!

if (cell == null)
{
 System.out.println("Cell is Empty in Column:" + cols);

} 
 else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
 {
 //code
 }

As a general matter, you should be careful while handling Cell.getCellType() function, since an empty cell could be either null or be a CELL_TYPE_BLANK.

I hope it helped.

Zakaria Shahed
  • 2,589
  • 6
  • 23
  • 52