我一直在尝试解决Null指针异常。我已经尝试过try
和catch
,但是它不起作用。
调试显示了迭代器开始的那一行,它引用了一些空值,并抛出了null pointer exception
,但我找不到它。
我已经尝试了所有但都解决不了。
错误是
java.lang.NullPointerException在 ReadDataUsingIterator.main(ReadDataUsingIterator.java:37)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.logging.Logger;
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.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadDataUsingIterator {
public static void main(String[] args) {
try {
File file = new File("C:\\Users\\Ashma\\Desktop\\Automation\\testdata.xlsx");
try {
@SuppressWarnings("unused")
FileInputStream fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XSSFWorkbook workbook = new XSSFWorkbook();
String sheetName = ("user_testdata");
Sheet sheet = workbook.getSheet(sheetName);
//XSSFSheet sheet = workbook.getSheetAt(0);
//iterate thru rows one by one
java.util.Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
java.util.Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//check cell type and format
if(cell !=null) {
switch(cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "t");
break;
}
}}
System.out.println(" ");
}
}
catch (NullPointerException ne)
{
ne.printStackTrace();
}
}
}
答案 0 :(得分:0)
您没有在读取Excel文件。您没有将FileInputStream传递给任何东西,只是创建了一个空的工作簿。此空工作簿没有名为umount /dev/$YOUR_DISK
e2fsck -f /dev/$YOUR_DISK
resize2fs -b /dev/$YOUR_DISK
e2fsck -f /dev/$YOUR_DISK
mount ...
的工作表,因此工作表对象为null。
您应该使用
user_testdata