私はJavaを初めて使用しており、Excelファイルを読み込もうとしています。 jarファイルが含ま:log4j-1.2.17
、poi-3.9
、poi-ooxml-3.5-beta5
、poi-ooxml-3.7-20101029
、poi-ooxml-schemas-3.7-beta1
、xmlbeans-2.5.0
、xmlbeans-xmlpublic-2.4.0
javaでExcelファイルを読み取っているときにNULLポインタ例外が発生する
package net.codejava.excel;
import java.io.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class SPACE_CreateThroughExcel{
public static void main(String[] args) throws Exception{
SPACE_CreateThroughExcel create = new SPACE_CreateThroughExcel();
create.read();
}
public void read(){
try {
String excelFilePath = "C:/Users/Rachana/workspace/SPACEOM/WebContent/Data/Data.xlsx";
InputStream is = new FileInputStream(new File(excelFilePath));
XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(is);
XSSFSheet ws = wb.getSheetAt(0);
int rowNum = ws.getLastRowNum() + 1;
System.out.println(rowNum);
int colNum = ws.getRow(0).getLastCellNum();
String[][] data = new String[rowNum][colNum];
for(int i=0; i< rowNum; i++){
XSSFRow row = ws.getRow(i);
for(int j=0;j<colNum;j++){
XSSFCell cell = row.getCell(j);
String value = cellToString(cell);
data[i][j] = value;
System.out.println(value);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String cellToString(XSSFCell cell){
int type;
Object result;
type = cell.getCellType();
switch(type){
case 0:
result = cell.getNumericCellValue();
break;
case 1:
result = cell.getStringCellValue();
break;
default:
throw new RuntimeException("No support");
}
return result.toString();
}
}
をこれが私のコードです。私は、後で私のJSPファイルで使用したい2D配列にExcelファイルのセルを読み込もうとしています。私はそれを実行すると、それは、次のエラーを示しています
at net.codejava.excel.SPACE_CreateThroughExcel.cellToString(SPACE_CreateThroughExcel.java:46)
at net.codejava.excel.SPACE_CreateThroughExcel.read(SPACE_CreateThroughExcel.java:30)
at net.codejava.excel.SPACE_CreateThroughExcel.main(SPACE_CreateThroughExcel.java:64)
私のファイルは、このようなものであるエクセル:
SPLD_DeviceID_Mfg SPLD_DeviceID_ModelNo SPLD_DeviceID_SrNo
4
apl 3
私のExcelファイルの一部のフィールドが空です。後で使用するために配列に空の値を格納する必要があります。私は整数と文字列の型だけをチェックしています。 nullの場合はどうすればいいですか?
可能な複製(http://stackoverflow.com/questions/218384/what-is-a- nullpointerexce-on-how-do-i-fix-it) – Fildor
セルにヌル値の条件がありません – yash
List complete stacktrace。 –