gpt4 book ai didi

java - 在java中读取xml时junit nullpointerException

转载 作者:行者123 更新时间:2023-12-01 10:57:02 24 4
gpt4 key购买 nike

由于某种原因,我在读取 java 代码中的 xml 文件时遇到困难。以下方法尝试使用 jdom 库设置并返回根元素。尽管当我运行 junit 测试并期望代码打印根时,它却给了我一个由 Wurzel = document.getRootElement(); 引起的空指针异常。 。我真的很感谢这里的一些帮助。顺便说一句,路径是正确的,文件没有奇怪的结构,也不包含错误。所以没有问题。我在这里阅读了一些有关 junit 测试和 nullpointerexception 的其他线程。没有太大帮助..提前谢谢

public void SetWurzel() {
builder = new SAXBuilder();
xmlFile = new File(Pfad);
try {
document = (Document) builder.build(xmlFile);
}
catch (IOException e) {
JOptionPane
.showMessageDialog(
null,
"Bitte prüfen Sie die Zugriffsrechte auf Ihre Datei und geben "
+ "Sie diese gegebenenfalls frei. ");
} catch (JDOMException e) {
JOptionPane
.showMessageDialog(
null,
"Bitte Prüfen Sie die Struktur der einzulesenden XMLDatei auf Fehlern"
+ " und fehlende Elemente.");
}
Wurzel = document.getRootElement();
}

public Element GetWurzel() {
return Wurzel;
}

这是我在其上运行的 junit 测试。

@Before
public void testSetPfad() {
this.input = new XML_Input();
}
@Test
public void testsetWurzel() {
input.setPfad("C:\\Users\\me\\workspace\\Workspace_uni\\Tests\\test1.xml");

input.SetWurzel();
System.out.println(input.GetWurzel().getText());

}

进行耳机建议的更改后,我得到以下异常堆栈跟踪:

java.io.FileNotFoundException: 

C:\Users\arm\workspace\Workspace_uni\Tests\test1.xml (Das System kann den angegebenen Pfad nicht finden)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:619)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:812)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:648)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:277)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:264)
at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1116)
at File_Processing_Component.XML_Input.SetWurzel(XML_Input.java:68)
at File_Processing_Component.XML_InputTest.testSetWurzelThrowFileNotFoundExceptionException(XML_InputTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

最佳答案

Wurzel = document.getRootElement(); 移至位于分配文档位置正下方的 try block 内。怀疑文档为空,并且由于某种原因您没有看到捕获的异常。

可能值得简单地使用 e.printStackTrace() 打印堆栈跟踪,而不是/以及 JOptionPane

并尝试路径:

    "/C:/Users/me/workspace/Workspace_uni/Tests/test1.xml"
"file:///C:/Users/me/workspace/Workspace_uni/Tests/test1.xml"

关于java - 在java中读取xml时junit nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33582426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com