2016-05-04 10 views
0

私はあなたのPOI Excelチュートリアルを行っています。 次のコードを実行すると、NullPointer例外が発生します。 Plzはansを提案します。 Thanku。サイクルのための最初の反復のために行のApache poiを使用してJavaでExcelファイルを作成する際に、NULLポインタ例外が発生しました。rowHeading.getCell(i).setCellStyle(stylerowHeading);

package main; 

import entities.*; 
import dao.*; 
import java.util.*; 
import org.apache.poi.hssf.usermodel.*; 
import org.apache.poi.hssf.util.*; 
import org.apache.poi.ss.usermodel.*; 
import org.apache.poi.ss.util.*; 
import java.io.*; 


public class Main { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     try{ 
      ProductModel pmodel = new ProductModel(); 
      HSSFWorkbook workbook= new HSSFWorkbook(); 
      HSSFSheet sheet=workbook.createSheet("List Products"); 
      //create heading 
      Row rowHeading = sheet.createRow(0); 
      rowHeading.createCell(1).setCellValue("Id"); 
      rowHeading.createCell(2).setCellValue("Name"); 
      rowHeading.createCell(3).setCellValue("Creation Date"); 
      rowHeading.createCell(4).setCellValue("Price"); 
      rowHeading.createCell(5).setCellValue("Sub Total"); 

      for(int i=0;i<6;i++) 
      { 
       CellStyle stylerowHeading= workbook.createCellStyle(); 
       Font font = workbook.createFont(); 
       font.setBold(true); 
       font.setFontName(HSSFFont.FONT_ARIAL); 
       font.setFontHeightInPoints((short)11); 
       stylerowHeading.setFont(font); 
       stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER); 
       rowHeading.getCell(i).setCellStyle(stylerowHeading); 

      } 

      //save to excel 
      FileOutputStream out = 
        new FileOutputStream("D:\\AVIATE\\excel\\listproducts.xls"); 
      workbook.write(out); 
      out.close(); 
      System.out.println("Excel written successfully........"); 

     }catch(Exception e){ 
      System.out.println("exception"); 
      e.printStackTrace(); 
     } 
    } 

} 
+0

forサイクルの最初の反復では、 'getCell(0)'を呼び出しますが、番号 '0'のセルは作成しません。最初のものは 'rowHeading.createCell(1)' – user3719857

+0

oh ...ありがとうございました。 –

答えて

0

、あなたはgetCell(0)を呼び出していますが、数0でセルを作成しないでください。最初のものはrowHeading.createCell(1)です。

関連する問題