抛出不可能的 NPE (java) [英] Impossible NPE being thrown (java)

查看:53
本文介绍了抛出不可能的 NPE (java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例代码中,第 4 行抛出了 NullPointerException.

In this example code a NullPointerException is being thrown on line 4.

1. private <O,T> void generate(Suggestion suggestion, GeneratorFactory<O, T> generatorFactory) {
2.     final Generator<O, T> generator = generatorFactory.getGenerator(suggestion);
3.     while (generator.hasNext()) {
4.         generator.doGenerate();
5.     }
6.     // post-generate stuff here
7. }

它只是偶尔发生,但在实时服务器上发生过几次,所以这不是某种幽灵"错误 - 它肯定需要修复.

It only happens occasionally, but it has happened a few times on a live server, so it's not some kind of "ghost" bug - it definitely needs fixing.

堆栈跟踪:

java.lang.NullPointerException
    at package.SuggestionServiceImpl.generate(SuggestionServiceImpl.java:4)

<小时>

GeneratorFactory 的代码:


Code for GeneratoryFactory:

@Override
public synchronized Generator<O, T> getGenerator(final Suggestion suggestion) {
    Generator<O, T> generator = generators.get(suggestion.getId());
    if (generator == null) {
        generator = construct(suggestion); // calls `new Generator()`
        generators.put(suggestion.getId(), generator);
    }
    return generator;
}

<小时>

我很困惑:


I'm confused by:

  • 为什么我们在第 3 行的if"语句中没有得到 NPE,如果 generator 为空?
  • 为什么堆栈跟踪从第 4 行开始,而不是从 doGenerate() 内的一行开始,如果那是异常的来源?(doGenerate() 的内部结构冗长而复杂——NPE 可能从这里开始,但为什么没有堆栈跟踪?)
  • How come we don't get a NPE in the 'if' statement on line 3, if it's generator that's null?
  • How come the stack trace starts at line 4, and not from a line inside doGenerate() if that's where the exception is coming from? (The internals of doGenerate() are lengthy and complex - it is possible that an NPE starts here, but why no stack trace?)

更新:

作为实验测试,我特意在 doGenerate() [开发环境] 中扔了一个 NPE,将堆栈跟踪与神秘的实时跟踪进行比较.它确实具有预期的额外堆栈帧.

As an experimental test, I deliberately threw a NPE inside doGenerate() [in dev environment] to compare the stack trace with the mystery live one. It does indeed have the anticipated extra stack frame.

java.lang.NullPointerException: DELIBERATE TEST EXCEPTION
    at package.Generator.doGenerate(Generator.java:71)
    at package.SuggestionServiceImpl.generate(SuggestionServiceImpl.java:4)

<小时>

运行时 JVM 是:


The runtime JVM is:

java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Oracle JRockit(R) (build R28.1.4-7-144370-1.6.0_26-20110617-2130-linux-x86_64, compiled mode)

推荐答案

我会给你一些理论上的选择:

I'll give you some theoretical options:

  1. 这可能是一件很平常的事情,比如报告了错误的行号.例如,由于二进制代码和您查看的源代码版本不同;
  2. 异常可能发生在其他地方,但您会看到一个被切断的堆栈跟踪.这可能是由于一些干预代码弄乱了堆栈跟踪(不需要魔法,只是普通的 Java),或者日志框架可能偶尔会弄乱它;
  3. 您遇到了 JRockit 中的一个实际错误,可能是它的 JIT.例如,可能有一些本地人清除实际上发生了乱序.
  1. it may be a mundane thing like a wrong line number reported. For example, due to different versions of binary code and the source code you are looking at;
  2. the exception may be happening somewhere else, but you see a severed stacktrace. This could be due to some intervenening code messing with the stacktrace (no magic neededed for this, just plain Java), or the logging framework may be messing it up occasionally;
  3. you are hitting an actual bug in JRockit, possibly its JIT. For example, there may be some locals clearing in effect which happens out of order.

我的建议:在循环中插入一个日志语句,但在 doGenerate 调用之上.您还可以明确记录 generator != null 的结果.这将帮助您消除最奇怪的解释.

My suggestion: insert a log statement within the loop, but above the doGenerate call. You may also explicitly log the result of generator != null. This will help you eliminate any but the most bizarre explanation.

这篇关于抛出不可能的 NPE (java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆