Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

java - Why the NPE using static method of DatatypeConverter?

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!
    }
}

Output

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 . . .

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!).

Both JREs fail with that NPE.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

which ultimately links to this issue https://github.com/javaee/jaxb-v2/issues/860 in the JAXB project on java.net.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...