在java中读取xml时的junit nullpointerexception

时间:2015-11-07 11:50:33

标签: java xml junit nullpointerexception jdom

由于某种原因,我在我的java代码中读取xml文件时遇到了困难。以下方法尝试使用jdom库设置和返回根元素。虽然当我运行junit测试并期望代码打印根时,它会给我一个由Wurzel = document.getRootElement();引起的nullpointer异常。我非常感谢这里的一些帮助。顺便说一句,路径是正确的,文件没有奇怪的结构,也没有错误。所以没有问题。我在这里读了一些关于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 test。

@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$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)

1 个答案:

答案 0 :(得分:1)

<a>移动到您分配文档的正下方的nav.pagedMenu ul li.hovered { -webkit-transform: translateX(1.5em); transform: translateX(1.5em); } nav ul li:hover a { transition: color, 1200ms; color: red; } 块内。怀疑该文档为空,并且由于某种原因您没有看到捕获的异常。

使用nav.pagedMenu ul li.hovered a{ transition: margin-left 500ms,transition: color 1200ms; margin-left:1.5em; color: red; } 而不是/ {以及Wurzel = document.getRootElement();

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

尝试路径:

try