2015-04-06 106 views
0

我正在Eclipse中導入帶poi的Excel 2007文件。我的問題是NullPointerException,但我不知道如何找出它來自哪裏。可能是類Data的實例有問題。這是代碼:類導致的實例nullPointerException

import java.io.*; 
import java.io.*; 
import java.util.ArrayList; 

import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.DateUtil; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.WorkbookFactory; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.xmlbeans.XmlOptions; 

class Data{ 
    public Data(){}; 
    public Data(double[] codnt, int ctgr1, int ctgr2) { 
    // super(); 
     this.codnt = codnt; 
     this.ctgr1 = ctgr1; 
     this.ctgr2 = ctgr2; 
    } 
    double codnt[]; 
    int ctgr1; 
    int ctgr2; 
} 
class Center{ 
    public Center(int cycle, Data[] c) { 
//  super(); 
     this.cycle = cycle; 
     this.c = c; 
    } 
    int cycle; 
    Data c[]; 
} 
public class KmeansCluster { 
    @SuppressWarnings({ "static-access", "null" }) 
    public static void main(String[] args)throws IOException,InvalidFormatException{ 
     InputStream is = new FileInputStream(new File("src//artificial_exp1.xlsx")); 
     Workbook wb = WorkbookFactory.create(is); 
     Sheet sheet = wb.getSheetAt(0); 
     ArrayList<Data> list = new ArrayList<Data>(); 
     int i = 0; 
     try { 
      for (Row row : sheet) { 
       Data dataTemp = new Data(); 
       // int i = 0; 
       int j = 0; 
       for (Cell cell : row) { 
        row.getCell(0).setCellType(cell.CELL_TYPE_NUMERIC); 
        dataTemp.codnt[j] = cell.getNumericCellValue(); 
            j++; 
        System.out.println("cell.getnumericcellvalue() is " + cell.getNumericCellValue() + "datatemp.condt[j] is " + dataTemp.codnt[j]); 
        System.out.println(" test passed !"); 
       } 
       dataTemp.ctgr1 = (int) dataTemp.codnt[j - 2]; 
       dataTemp.ctgr2 = (int) dataTemp.codnt[j - 1]; 
       list.add(dataTemp); 
       i++; 
      } 
      Data data1 = (Data) list.get(0); 
      System.out.println(data1.codnt[0] 
        + " is the content of the first blank."); 
     } catch (Exception e) { 
      System.out.println(e.toString()); 
     } 
    } 

} 
+2

爲了幫助找到問題,完整的異常文本和堆棧跟蹤將會有所幫助。 – user700390

回答

4

Data的無參數的構造離開codnt初始化。您可以通過在通用異常塊中打印堆棧跟蹤來發現此問題

+0

非常感謝~~~ @Reimeus –

相關問題