Java 破译包含大量JUnit的堆栈跟踪
请注意,这不是的副本Java 破译包含大量JUnit的堆栈跟踪,java,junit,stack-trace,Java,Junit,Stack Trace,请注意,这不是的副本 我一直在编写一些作为JUnit运行的代码。我阅读了上面链接的线程,它讨论了堆栈跟踪中错误的位置通常最接近底部。然而,由于我的代码是作为JUnit测试运行的,堆栈跟踪中注入了很多我认为是作为单元测试运行的代码的结果,因此错误不能直接跟踪到堆栈跟踪的底部,就像上面的帖子所说的那样。(如果查看堆栈跟踪中的根本原因,它表明NullPointerException发生在的API.ItemFetcher.(ItemFetcher.java:39)。这意味着在ItemFetcher类的
我一直在编写一些作为JUnit运行的代码。我阅读了上面链接的线程,它讨论了堆栈跟踪中错误的位置通常最接近底部。然而,由于我的代码是作为JUnit测试运行的,堆栈跟踪中注入了很多我认为是作为单元测试运行的代码的结果,因此错误不能直接跟踪到堆栈跟踪的底部,就像上面的帖子所说的那样。(如果查看堆栈跟踪中的根本原因,它表明
NullPointerException
发生在的API.ItemFetcher.(ItemFetcher.java:39)
。这意味着在ItemFetcher
类的第39行,出现了NullPointerException
请记住,JUnit只调用您现有的类/方法。因此,请始终查找您的代码在堆栈跟踪中的位置。堆栈跟踪是一个强大的工具,您所引用的代码为如何使用它提供了非常好的建议 但是堆栈跟踪只是一个“工具”。你必须做这项工作。你必须有逻辑地思考 你不能说像“line”ItemFetcher这样的废话。java:xyz“一直都能工作,所以这不可能是问题”。那是我投票否决你的时候;)你删除了那句话(谢谢!),我收回了我的否决票 下面是我如何处理这个问题的:
java.lang.NullPointerException
位于API.ItemFetcher.(ItemFetcher.java:39)
首先查看Tests.Tests.test
,第14行。测试中的这一行导致了失败。对于NullPointerException,您通常希望在回溯的第一行查找正在访问的未初始化对象:API.ItemFetcher。(ItemFetcher.java:39)
。然后反向工作以确定对象变量应在何处初始化。您的问题是“如何解释堆栈跟踪?”“。这是一个规范性问题。尽管你抗议这不是一个重复,否认你没有提供任何理由,但这是关于堆栈跟踪的规范性问题的重复。现在我意识到了更多。这一次不起作用,但以前起作用的原因是我有一个处于TODO状态的构造函数,这导致了一个错误。因此,JSoup抛出了一个空指针异常,因为它正在搜索不存在的东西。
java.lang.NullPointerException
at API.ItemFetcher.<init>(ItemFetcher.java:39)
at API.MarketConstants.<clinit>(MarketConstants.java:24)
at API.ItemFetcher.setOtherMonetaryValues(ItemFetcher.java:153)
at API.ItemFetcher.<init>(ItemFetcher.java:53)
at Tests.Tests.test(Tests.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
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.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:86)
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)
-src
-API
-Item.java
-ItemFetcher.java
-MarketConstants.java
-IrrelevantFolder
-Tests
-Tests.java
java.lang.NullPointerException
at API.ItemFetcher.<init>(ItemFetcher.java:39)
<-- Your problem is an uninitialized variable at line 39!