使用Apache POI时出现空指针异常

时间:2016-07-19 06:05:37

标签: selenium-webdriver apache-poi

我正在尝试从web输入值,包含2列和9行,这样我就可以从网上获得10个产品的价格和描述。 但我得到Null指针异常。请任何人帮我解决此错误。

$(function() {
    $('.associationbox').on('click', function() {
        if ($('.associationbox').prop('checked')) {
            alert('checked');
        }
    });
});

获得的异常是:

线程“main”java.lang.NullPointerException中的异常     at samples.learningold.main(learningold.java:97)

1 个答案:

答案 0 :(得分:0)

首先,您的ProductTitle Xpath不会从列表中获取所有产品名称。我也更新了。

以下代码效果很好。

package samples;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.poi.EncryptedDocumentException; 
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
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.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class X
{
@SuppressWarnings("resource")
public static  void main(String[] args) throws InterruptedException,    ParseException, IOException, EncryptedDocumentException, InvalidFormatException
  {
        System.out.println("selenium");

        WebDriver webdriver = new FirefoxDriver();
        webdriver.manage().window().maximize();
        webdriver.get("http://www.snapdeal.com");
        webdriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        List<WebElement> alllinks = webdriver.findElements(By.tagName("a"));
        int linkcnt = alllinks.size();
        System.out.println("total links=" +linkcnt);

        Actions action = new Actions(webdriver);
        WebElement COG = webdriver.findElement(By.xpath("//span[text()='Computers, Office & Gaming']"));
        WebElement EHD = webdriver.findElement(By.xpath("//span[text()='External Hard Drives']"));
        action.moveToElement(COG).build().perform();
        Thread.sleep(5000);
        EHD.click();

        webdriver.findElement(By.xpath("//label[@for='Capacity_s-1 TB']")).click();
        Thread.sleep(5000);
        webdriver.findElement(By.xpath("//a[contains(text(),'500 GB')]/..")).click();
        Thread.sleep(5000);
        webdriver.findElement(By.xpath("(//span[@class='price-collapse-arrow'])[1]/..")).click();
        Thread.sleep(5000);
        WebElement totalitems = webdriver.findElement(By.xpath("//span[@class='category-count']"));

        String totalitemsvalue=totalitems.getText();
        System.out.println(totalitemsvalue);
        String value=totalitemsvalue.replaceAll(" Items","");
        System.out.println(value);


        try
        {

            List<WebElement> productprice = webdriver.findElements(By.xpath("//div[@class='product-tuple-description']/div[2]"));
            List<WebElement> productTitle = webdriver.findElements(By.xpath("//div[@class='product-tuple-description']/div[1]"));
            int count=productprice.size();
            int count1=productTitle.size();
            System.out.println(count);
            System.out.println(count1);

            String[] productPriceList = new String[count];
            String[] productTitleList = new String[count];


            Thread.sleep(2000);
            for(int k =0; k<count; k++)
            {
                System.out.println(productprice.get(k).getText());
                productPriceList[k]=productprice.get(k).getText();

                System.out.println(productTitle.get(k).getText());
                productTitleList[k]=productTitle.get(k).getText();

           }

            File file= new File("C:\\Users\\XX\\Downloads\\snapdeal.xlsx");  // give your file path
            FileInputStream inputStream = new FileInputStream(file);
            Workbook sampleWorkbook=null;
            sampleWorkbook=new XSSFWorkbook(inputStream);
            Sheet sheet = sampleWorkbook.getSheet("Sheet1");

           for(int t=0;t<count;t++)
           {
               Row row = sheet.createRow(t);
               Cell cell = row.createCell(0);    // create column 1
               cell.setCellValue(productPriceList[t].toString());

               Cell cell1 = row.createCell(1);   // create column 2
               cell1.setCellValue(productTitleList[t].toString());

               inputStream.close();
               FileOutputStream outputStream = new FileOutputStream(file);
               sampleWorkbook.write(outputStream);
               System.out.println("Data written to Excel successful");   
               outputStream.close();
        }
   }
        catch(Exception e)
        {
            System.out.println(e);
        }

  }

}