0

I just updated my NetBeans from 8.0 to 8.0.1 and my JDK from 1.8.0 to 1.8.0u20. I guess most likely my problem is caused by the jdk-update.

Before the updates I was able to compile this line of code:

int sum = myIntegerList.stream().reduce(0, (a, b) -> a + b, (c, d) -> c + d);

Right now this wont compile any longer. NetBeans tells me that the "main class could not be found or loaded".

Now I have to write it like this:

Integer sum = myIntegerList.stream().reduce(0, (a, b) -> a + b, (c, d) -> c + d);

or

int sum = (int) myIntegerList.stream().reduce(0, (a, b) -> a + b, (c, d) -> c + d);

or

int sum = myIntegerList.stream().reduce(0, (Integer a, Integer b) -> a + b, (Integer c, Integer d) -> c + d);

Does anyone know why this doesn't work any longer? Am I doing something wrong?

Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49
jbw
  • 5
  • 1
  • 1
    "main class could not be found or loaded" - That is not a compilation error, it just means that NetBeans doesn't know which class contains the `public static void main(String[] args)` method, or you're trying to run a class that has no `main` method. It's not the actual cause of the problem. – Jesper Sep 11 '14 at 08:26
  • 1
    I tried compiling your code on the command line with JDK 8u20. The compiler crashes with a `NullPointerException`! It looks like a bug in the JDK 8u20 compiler. – Jesper Sep 11 '14 at 08:32
  • Glad to hear that most likely it's not my mistake. Thank you! – jbw Sep 11 '14 at 08:33
  • Your first line compiles and runs fine with 8u20... @Jesper what code did you run? – assylias Sep 11 '14 at 08:51
  • I just compiled this class with javac from 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 myIntegerList = Arrays.asList(1, 2, 3, 4); int sum = myIntegerList.stream().reduce(0, (a, b) -> a+b, (a, b) -> a + b); } }` getting the same NPE as Jesper. – jbw Sep 11 '14 at 09:04
  • ErrorStream: An exception has occurred in the compiler (1.8.0_20). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you. java.lang.NullPointerException at com.sun.tools.javac.code.Types.isConvertible(Types.java:290) at com.sun.tools.javac.comp.Check.assertConvertible(Check.java:922) at com.sun.tools.javac.comp.Check.checkMethod(Check.java:876) at com.sun.tools.javac.comp.Attr.checkMethod(Attr.java:3838) – jbw Sep 11 '14 at 09:16
  • Can be compiled with `jdk1.8.0_40b02`, so I guess it will be already fixed in the next release. (So there’s no need for a bug report then). I just download `jdk1.8.0_40b04` for verification… – Holger Sep 11 '14 at 09:20
  • I just downloaded jdk1.8.0_40b04. I can compile the class "Test.java" now, but I cant run it (java Test). Java cant find the main class. – jbw Sep 11 '14 at 10:25
  • @jbw: that’s a completely different issue. Check that your method really has the signature `public static void main(String[])` and that there is no other `String` class is in scope (read that the signature really is `public static void main(java.lang.String[])`) – Holger Sep 11 '14 at 11:02
  • It is exactly the class I have posted in comment #5. Correct psvm-method. NetBeans sees no syntax-mistakes in my class. No other String-classes in scope. – jbw Sep 11 '14 at 13:05

1 Answers1

1

This seems to be a bug in the jdk version 1.8.0u20 which has been fixed already in the early 1.8.0u40. However, Netbeans seems to have its own copy of the problematic compiler included which interferes when attempting to run the compiled code. So it’s not enough to change the project’s jdk and the Netbeans environment to a different jdk, the NullPointerException is still being reported.

The only solution is to disable the automatic compilation (Project Properties/Build/Compiling → Compile on Save) and do a clean build of the project. Then it should run without the “can’t find the main class” error. (The NullPointerException will still occur in the background from time to time but it doesn’t hinder compiling and running anymore)

Holger
  • 285,553
  • 42
  • 434
  • 765
  • I can compile it using your solution. I hope NetBeans will fix this soon. Thanks! – jbw Sep 12 '14 at 07:19