简体   繁体   English

为什么NumberFormat.format会抛出NullPointerException?

[英]Why does NumberFormat.format throw an NullPointerException?

I've just found a very strange NullPointerException . 我刚发现一个非常奇怪的NullPointerException First, I create a NumberFormat like this (note that the default Locale would be Germany, I don't know if that helps): 首先,我创建一个这样的NumberFormat (注意默认的Locale是德国,我不知道是否有帮助):

NumberFormat angleFormat = NumberFormat.getNumberInstance(Locale.UK);
angleFormat.setMaximumFractionDigits(5);
angleFormat.setMinimumFractionDigits(0);

Then, I tried to format a double with it. 然后,我试着用它格式化一个双。 This is done with a new Thread created by a Lambda, while angleFormat is declared at the method containing the Lambda. 这是通过Lambda创建的新线程完成的,而angleFormat是在包含Lambda的方法中声明的。 The code where the Exception is thrown looks like this: 抛出异常的代码如下所示:

con.println("D" + moveId + (state.isEnemyInSightOf(e) ? "+" : "-")
        + angleFormat.format(e.getAngle()) // line 123 - error is here
        + (state.isMissileInSightOf(e) ? "+" : "-")
        + angleFormat.format(e.getSight())
        + (e.getLastShot() >= 10 || e.getLastShot() <= -1 ? "+" : "-")
        + angleFormat.format(e.getLives()));

e.getAngle() returns a double , so it can't return null. e.getAngle()返回一个double ,因此它不能返回null。 However, I get this Exception: 但是,我得到了这个例外:

Exception in thread "Thread-1" java.lang.NullPointerException
    at java.text.DecimalFormat.fastDoubleFormat(Unknown Source)
    at java.text.DecimalFormat.fastFormat(Unknown Source)
    at java.text.NumberFormat.format(Unknown Source)
    at server.game.Simulator.lambda$0(Simulator.java:123)
    at server.game.Simulator$$Lambda$3/23162747.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

I'm sure that e isn't null because of the Exception's stacktrace, it would a) be thrown one line earlier and b) not at java.text.DecimalFormat.fastDoubleFormat 我确定e不是null因为Exception的堆栈跟踪,它会a)先抛出一行而b)不抛出java.text.DecimalFormat.fastDoubleFormat

Why is there a NullPointerException beeing thrown sometimes , and sometimes it works without problems? 为什么有时会抛出NullPointerException ,有时它可以正常工作? And what does that mean? 这意味着什么? The error seems to be reproducable, but not very often. 该错误似乎是可重现的,但不是很常见。

From the comments: I create the threads in a loop with each one a different e but the same NumberFormat 从评论中:我在一个循环中创建线程,每个线程都有不同的e但是相同的NumberFormat

This appears to be the source of intermittent issues that your code is experiencing. 这似乎是您的代码遇到的间歇性问题的根源。 According to the documentation of NumberFormat , the class is not thread-safe, so concurrent access must be synchronized externally: 根据NumberFormat文档 ,该类不是线程安全的,因此必须在外部同步并发访问:

Number formats are generally not synchronized. 数字格式通常不同步。 It is recommended to create separate format instances for each thread. 建议为每个线程创建单独的格式实例。 If multiple threads access a format concurrently, it must be synchronized externally. 如果多个线程同时访问格式,则必须在外部进行同步。

From the JavaDoc for DecimalFormat 来自JavaDoc for DecimalFormat

Synchronization 同步

Decimal formats are generally not synchronized. 十进制格式通常不同步。 It is recommended to create separate format instances for each thread. 建议为每个线程创建单独的格式实例。 If multiple threads access a format concurrently, it must be synchronized externally. 如果多个线程同时访问格式,则必须在外部进行同步。

By the way, which version of Java is this? 顺便问一下,这个版本的Java是什么? I don't see a DecimalFormat.fastFormat() method in the docs for either Java 6 or 7. 我没有在Java 6或7的文档中看到DecimalFormat.fastFormat()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM