问题描述
我已经盯着这个短代码太久了,我一辈子都看不到它是如何在第 6 行抛出 NullPointerException
的.为什么是 NPE?
I have stared at this short code too long, and cannot for the life of me see how it can throw a NullPointerException
on line 6. Why the NPE?
class ConvertTest {
public static void main(String[] args) throws Exception {
byte[] b = "Ha".getBytes("UTF-8");
System.out.println("bytes: " + b.length);
javax.xml.bind.DatatypeConverter.printBase64Binary(b); // NPE!
}
}
输出
bytes: 2
Exception in thread "main" java.lang.NullPointerException
at javax.xml.bind.DatatypeConverter.printBase64Binary(DatatypeConverter.java:547)
at ConvertTest.main(ConvertTest.java:6)
Press any key to continue . . .
更新
虽然许多错误报告都指向 1.7 变体,但我惊讶地发现 Eclipse 被配置为使用 1.6.0_32
&TextPad 找到了 1.6.0-b105
的版本(甚至没有意识到已安装!).
Update
While many of the bug reports are pointing to a 1.7 variant, I was astonished to see that Eclipse is configured to use 1.6.0_32
& TextPad has found a version of 1.6.0-b105
(didn't even realize that was installed!).
两个 JRE 都因该 NPE 失败.
Both JREs fail with that NPE.
推荐答案
现在 JDK7 中的 JAXB 似乎存在错误,正如 Camel 上的这个问题所证明的那样:
It seems as though there are bugs in JAXB within JDK7 right now, as evidenced by this issue on Camel:
https://issues.apache.org/jira/browse/CAMEL-4893
最终链接到这个问题 https://github.com/javaee/jaxb-v2/issues/860 在 java.net 上的 JAXB 项目中.
which ultimately links to this issue https://github.com/javaee/jaxb-v2/issues/860 in the JAXB project on java.net.
我不完全确定您是否遇到了同样的事情,但也许可以尝试在那里使用 JDK6 和最新的 JAXB 版本,看看是否会发生相同的 NPE.
I'm not entirely sure if you are running into this same thing or not, but perhaps try using JDK6 with the latest JAXB version there and see if the same NPE happens or not.
这篇关于为什么 NPE 使用 DatatypeConverter 的静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!