2016-07-19 3 views
-1

2 열 9 행을 갖는 웹에서 Excel 값을 입력하려고하므로 웹에서 10 가지 제품의 가격과 설명을 얻을 수 있습니다. 그러나 Iam이 Null 포인터 예외를 받고 있습니다. 이 오류를 해결할 때 아무도 도와주세요. 무엇입니까Apache POI를 사용할 때 null 포인터 예외

package samples; 


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

//import org.apache.commons.io.FileUtils; 

//import org.apache.poi.hssf.usermodel.HSSFRow; 


import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 

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.ss.usermodel.WorkbookFactory; 

import java.text.ParseException; 
import java.util.List; 
import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 
//import org.openqa.selenium.OutputType; 
//import org.openqa.selenium.TakesScreenshot; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.interactions.Actions; 






public class A1 { 



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

      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-desc-rating title-section-collapse']")); 
      int count=productprice.size(); 
      int count1=productTitle.size(); 
      System.out.println(count); 
      System.out.println(count1); 

      //int i=9; 
     // int j=9; 
      Thread.sleep(2000); 
     for (count=0;count<10;count++) 
      { 
      productprice = webdriver.findElements(By.xpath("//div[@class='product-tuple-description']/div[2]")); 
     // File srcfile=((TakesScreenshot)webdriver).getScreenshotAs(OutputType.FILE); 
     // FileUtils.copyFile(srcfile, new File("c:\\screenshot.png")); 
      Thread.sleep(2000); 
      String RupeesValue= productprice.get(count).getText(); 
      System.out.println(RupeesValue); 
      Thread.sleep(2000); 
       productTitle = webdriver.findElements(By.xpath("//div[@class='product-tuple-description']/div[1]")); 
      Thread.sleep(2000); 
       String TitleValue= productTitle.get(count1).getText(); 
       System.out.println(TitleValue); 
       Thread.sleep(2000); 

      FileInputStream fis = new FileInputStream("C:\\Users\\aa74231\\Desktop\\abc.xlsx"); 
      Workbook wb = WorkbookFactory.create(fis); 
       Sheet sheet = wb.getSheet("Sheet1"); 
     //  Sheet sheet1 = wb.getSheet("Sheet1"); 

       for (int i=0;i<2;i++) 
       { 
       Row row=sheet.getRow(count); 
       // Row row1=sheet.getRow(j); 
       Cell cell = row.createCell(count); 
       // Cell cell1 = row1.createCell(count1); 
      cell.setCellType(cell.CELL_TYPE_STRING); 
      //cell1.setCellType(cell1.CELL_TYPE_STRING); 
      if(i==0){ 
      cell.setCellValue(productprice.get(count).getText()); 
      } 
      else 
      { 
       cell.setCellValue(productTitle.get(count).getText()); 
      } 
      //cell1.setCellValue(productTitle.get(count1).getText()); 
      FileOutputStream fos=new FileOutputStream("C:\\Users\\aa74231\\Desktop\\abc.xlsx"); 
      wb.write(fos); 
      fos.close(); 

      wb.close(); 
      } 
      } 




      } catch(Exception e){ 
       e.printStackTrace(); 
     } 

} 
} 

예외입니다 : 스레드에서

예외 "주"samples.learningold.main에서 java.lang.NullPointerException이 (learningold.java:97)의

+4

문제를 해결하는 데 도움이됩니다 예외를 게시하시기 바랍니다. –

+0

가능한 [NullPointerException이란 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it)) – whitesite

+1

전체 예외 메시지를 포함하도록 답을 수정하십시오 ..... –

답변

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

    } 

}