【发布时间】:2014-11-05 02:47:53
【问题描述】:
我刚刚将我的 NetBeans 从 8.0 更新到 8.0.1,我的 JDK 从 1.8.0 更新到 1.8.0u20。我想我的问题很可能是由 jdk-update 引起的。
在更新之前我能够编译这行代码:
int sum = myIntegerList.stream().reduce(0, (a, b) -> a + b, (c, d) -> c + d);
现在这不会再编译了。 NetBeans 告诉我“无法找到或加载主类”。
现在我必须这样写:
Integer sum = myIntegerList.stream().reduce(0, (a, b) -> a + b, (c, d) -> c + d);
或
int sum = (int) myIntegerList.stream().reduce(0, (a, b) -> a + b, (c, d) -> c + d);
或
int sum = myIntegerList.stream().reduce(0, (Integer a, Integer b) -> a + b, (Integer c, Integer d) -> c + d);
有人知道为什么这不再起作用了吗?我做错了吗?
【问题讨论】:
-
"找不到或加载主类" - 这不是编译错误,它只是意味着 NetBeans 不知道哪个类包含
public static void main(String[] args)方法,或者您正在尝试运行一个没有main方法的类。这不是问题的真正原因。 -
我尝试使用 JDK 8u20 在命令行上编译您的代码。编译器因
NullPointerException而崩溃!它看起来像是 JDK 8u20 编译器中的一个错误。 -
很高兴听到这很可能不是我的错误。谢谢!
-
您的第一行在 8u20 下编译并运行良好...@Jesper 您运行了什么代码?
-
我刚刚用 javac 从 1.8.0u20 编译了这个类:
import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class Test { public static void main(String[] args) { List<Integer> myIntegerList = Arrays.asList(1, 2, 3, 4); int sum = myIntegerList.stream().reduce(0, (a, b) -> a+b, (a, b) -> a + b); } }得到了与 Jesper 相同的 NPE。
标签: java lambda java-8 reduce autoboxing