在 java 中读取 xml 时出现 junit nullpointerexception

junit nullpointerexception while reading xml in java

出于某种原因,我无法在我的 java 代码中读取 xml 文件。以下方法尝试使用 jdom 库设置和 return 根元素。虽然当我 运行 一个 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 测试 运行ning 在上面。

@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());

    }

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

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.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.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.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 块内,直接位于您分配文档的位置下方。怀疑该文档为空,并且出于某种原因您没有看到捕获的异常。

使用 e.printStackTrace() 而不是 of/as 以及 JOptionPane

打印出堆栈跟踪可能是值得的

并尝试路径:

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