我已经用类似的问题检查了这里的每一个可能的问题,没有一个解决方案有效。我觉得我错过了一些非常明显的东西,但我就是看不出来。
我在Windows10上,使用Eclipse2019,以及最新的Apache POI jar文件(不是maven),其中包含xmlbeans版本3.1.0。我想读取一个xlsx文件,但每次运行代码(这是一个基本的示例,我已经在多个网站和视频中成功地使用并运行)时,我都会在创建工作簿的行处收到一个“NullPointerException”:
import java.io.File;
//import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
//import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class excelImportTest {
public static void main (String args[]) throws IOException, InvalidFormatException {
// Linking file to Workbook
// ****Where exception is called every time****
XSSFWorkbook wb = new XSSFWorkbook(new File("C:\\Users\\Owner\\Downloads\\TestData.xlsx"));
// Pulling sheet from Workbook
XSSFSheet sheet = wb.getSheetAt(0);
// Get iterator to all the rows in current sheet
Iterator<Row> rowIterator = sheet.iterator();
// Traversing over each row of XLSX file
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
default :
}
}
}
wb.close();
以下是我收到的异常:
Exception in thread "main" java.lang.ExceptionInInitializerError
at sun.misc.Unsafe.ensureClassInitialized(Native Method)
at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(Unknown Source)
at sun.reflect.ReflectionFactory.newFieldAccessor(Unknown Source)
at java.lang.reflect.Field.acquireFieldAccessor(Unknown Source)
at java.lang.reflect.Field.getFieldAccessor(Unknown Source)
at java.lang.reflect.Field.get(Unknown Source)
at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:775)
at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument.<clinit>(Unknown Source)
at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source)
at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:86)
at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61)
at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:684)
at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:180)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:288)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:325)
at excelImportTest.main(excelImportTest.java:62)
Caused by: java.lang.RuntimeException: Could not instantiate SchemaTypeSystemImpl (java.lang.reflect.InvocationTargetException): is the version of xbean.jar correct?
at schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder.loadTypeSystem(Unknown Source)
at schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder.<clinit>(Unknown Source)
... 16 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
... 18 more
Caused by: java.lang.NullPointerException
at org.apache.xmlbeans.impl.schema.ClassLoaderResourceLoader.getResourceAsStream(ClassLoaderResourceLoader.java:33)
at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl$XsbReader.getLoaderStream(SchemaTypeSystemImpl.java:2249)
at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl$XsbReader.<init>(SchemaTypeSystemImpl.java:1522)
at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.initFromHeader(SchemaTypeSystemImpl.java:273)
at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.<init>(SchemaTypeSystemImpl.java:185)
... 22 more
我已经尝试了我能想到的每一种选择组合和我在网上找到的建议:
我想你没有正确地添加罐子。
首先转到这个url:https://archive.apache.org/dist/poi/release/bin/并从那里下载poi-bin-4.1.2-20200217.zip
。
现在提取zip并将这些jar添加到用红色标记的项目中。
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Testing {
public static void main(String args[]) throws IOException, InvalidFormatException {
// Linking file to Workbook
// ****Where exception is called every time****
XSSFWorkbook wb = new XSSFWorkbook(new File("C:\\Users\\Anish\\Downloads\\TestData.xlsx"));
System.out.println("Workbook loaded");
// Pulling sheet from Workbook
XSSFSheet sheet = wb.getSheetAt(0);
// Get iterator to all the rows in current sheet
Iterator<Row> rowIterator = sheet.iterator();
// Traversing over each row of XLSX file
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;
default:
}
}
}
wb.close();
}
}
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
问题内容: 根据文档,该方法返回: 如果参数是,则字符串等于; 否则,返回的值。 但是,当我尝试这样做时,怎么会这样: 它会抛出NPE吗?(如果你不相信,请自己尝试!) 怎么会这样呢?文件在骗我吗?这是Java中的主要错误吗? 问题答案: 问题是方法已重载: String.valueOf(Object) String.valueOf(char[]) Java规范语言要求在这种情况下,选择最具体的重
我正在尝试使用JDT SearchEngine查找对给定对象的引用。但在调用org.eclipse.jdt.core.search.SearchEngine的“search”方法时,我得到了一个“NullPointerException”。 以下是错误跟踪: Java . lang . nullpointerexception at org . eclipse . JDT . internal .
我有一个PrimefaceDataTable内的一个手风琴面板与动态标签数。可以通过单击列标题按每个列对DataTable进行排序。DataTable在更新行时在Primeface的DataTable类中抛出NullPointerExcture。只有在之前对表进行排序后才会发生错误。我已经在手风琴外面测试了同样的表,它没有显示错误。 我做错了什么? 我的xhtml: 它抛出的异常是: 来自后备be
首先,我知道以前有人问过这个问题,我花了一个小时查看其他解决方案,主要是从-getActionBar()返回null,但没有一个解决方案有效,所以另一双眼睛可能可以做一些事情。 其他类从其继承的NavigationDrawer类: MainActiviy扩展了上述内容: 导航抽屉。xml styles.xml Activity_Main content_main
我试图从这个网址下载图像: https://hme_player_pictures.s3.amazonaws.com/test-512813ed3b83286c72f376c7-thumb100.jpg 这里是堆栈跟踪: 代码: 无法理解它为什么抛出NullPointerException。以上url在浏览器中确实有效。
问题内容: 以下代码抛出: 因为它是静态的,所以我的编译器是否调用null?那没有任何意义! 发生了什么? 问题答案: 这里有两个问题在起作用: 不按照你的想法去做 在这种情况下返回 从到的分配会导致自动取消装箱 由于是被抛出 要解析,你可以使用例如。