2017-09-18 5 views
-2

タイトルと価格をExcelファイルに書き込もうとしています。私は列を作成していますが、私は行番号48でエラー 'NULLポインタの除外'エラーを主催しています。私は何が主な理由です。私は行番号48で書いてください、sheet1.getRow(0).createCell(0).getStringCellValue( 'Naqash'); Nullポインタエラーは表示されません。ヌルポインタ例外新しいセルを作成してExcelにデータを書き込もうとすると例外が発生する

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

     } 

    } 

} 
+2

[NullPointerExceptionとは何ですか?どうすれば修正できますか?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix - ) – JFPicard

答えて

0

行とセルをデータを挿入する前に作成してください。たとえば:あなたはすでに問題は、細胞がnullの場合は、テストすることはありませんということです

+0

あなたは私の日を作りました、ありがとうございました@pomah :) – naqash

+0

@naqashので、あなたが=あなたを助けたら私の答えを受け入れることができます)幸運! –

+0

、はい私は投票しましたが、私の評判は投票の反映を許可していません。 – naqash

0

前にそれを作成した場合

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

あなたはgetRow(index)で行を取得することができます!一般的な問題として

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

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

Cell.getCellType()機能を処理しながら、空のセルがnullたりすることCELL_TYPE_BLANKのいずれかであることから、あなたは、注意しなければなりません。

私はそれが助けてくれることを願っています。

+0

http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Row.MissingCellPolicy.html – zsbappa

+0

あなたの返信ありがとう、私は解決策を得て、助けてください:) – naqash

関連する問題