无害行

时间:2015-08-08 18:39:07

标签: java nullpointerexception

我们的JUnit测试有时会因此堆栈跟踪而失败:

Caused by: java.lang.NullPointerException
    at org.languagetool.rules.patterns.PatternRuleMatcher.formatMatches(PatternRuleMatcher.java:300)
    at org.languagetool.rules.patterns.PatternRuleMatcher.createRuleMatch(PatternRuleMatcher.java:158)
    [...]

第300行是:

if (j >= positions.size()) {

jintpositionsList<Integer>,都是局部变量。这怎么会导致NullPointerException?我在代码中看不到任何地方 positions始终设置为null。它使用new ArrayList<>(tokens.length + 1)进行初始化。实际上,如果我在发生异常之前添加if (positions == null) { ... }一行,IntelliJ甚至会将positions == null标记为警告并告诉我将始终为假。

该软件使用线程,但由于这两个变量都是本地的,因此无关紧要。问题很少发生,我必须运行测试30-40次才能解决问题。

这里发生了什么,Java提供的线路可以关闭吗? (我确定我正在查找正确的文件和正确的版本,文件也不会经常更改。)

这是Java 1.8.0_51。完整代码可以在https://github.com/languagetool-org/languagetool/blob/master/languagetool-core/src/main/java/org/languagetool/rules/patterns/PatternRuleMatcher.java#L300

看到

更新:按要求完成堆栈跟踪:

testRules(org.languagetool.rules.ca.CatalanPatternRuleTest)  Time elapsed: 29.978 sec  <<< ERROR!
java.lang.RuntimeException: Error analyzing sentence: '<S> Va[anar/VAIP3S00,Va/_GV_] haver[haver/VAN00000,heure/VMN00000,haver/_GV_] una[un/DI0FS0,una/_GN_FS] reunió[reunió/NCFS000,reunió/_GN_FS].[</S>./_PUNCT]'
    at org.languagetool.rules.patterns.PatternRule.match(PatternRule.java:191)
    at org.languagetool.rules.patterns.PatternRuleTest.getMatches(PatternRuleTest.java:462)
    at org.languagetool.rules.patterns.PatternRuleTest.testBadSentences(PatternRuleTest.java:294)
    at org.languagetool.rules.patterns.PatternRuleTest.testGrammarRulesFromXML(PatternRuleTest.java:252)
    at org.languagetool.rules.patterns.PatternRuleTest.runTestForLanguage(PatternRuleTest.java:187)
    at org.languagetool.rules.patterns.PatternRuleTest.runGrammarRuleForLanguage(PatternRuleTest.java:103)
    at org.languagetool.rules.patterns.PatternRuleTest.runGrammarRulesFromXmlTest(PatternRuleTest.java:91)
    at org.languagetool.rules.ca.CatalanPatternRuleTest.testRules(CatalanPatternRuleTest.java:28)
    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 junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
Caused by: java.lang.NullPointerException
    at org.languagetool.rules.patterns.PatternRuleMatcher.formatMatches(PatternRuleMatcher.java:300)
    at org.languagetool.rules.patterns.PatternRuleMatcher.createRuleMatch(PatternRuleMatcher.java:158)
    at org.languagetool.rules.patterns.PatternRuleMatcher.match(PatternRuleMatcher.java:136)
    at org.languagetool.rules.patterns.PatternRule.match(PatternRule.java:187)
    ... 27 more

2 个答案:

答案 0 :(得分:4)

如果您完全确定用于构建库的源文件,则只能依赖堆栈跟踪中的行号。

在您的情况下,相关文件最近已更改。事实上,第300行的内容已经改变as seen by the diff

故事的道德 - 除非您拥有原始来源,否则不要信任堆栈跟踪行号。

答案 1 :(得分:0)

Java 7没有出现这个问题,Java 1.8.0_60不再发生这种情况。所以这实际上是一个JVM错误,也许这个错误:http://bugs.java.com/view_bug.do?bug_id=8080156