4

I am getting the following error when I try to build my Java 1.8 project:

COMPILATION ERROR : 
-------------------------------------------------------------
error: incompatible types: Path is not a functional interface
1 error

There is no line number associated with the error, but Netbeans is showing the red squiggly line under the package declaration for one of the classes in the project. That class doesn't even use Path though. It extends an abstract class in the same package that does use Path, if that's relevant.

Has anyone seen this before? Any thoughts on how to fix this?

Jess
  • 217
  • 1
  • 3
  • 14
  • 3
    Since you haven't posted any code, I can't say for sure, but this looks relevant [http://stackoverflow.com/questions/23682243/lambda-can-only-be-used-with-functional-interface](http://stackoverflow.com/questions/23682243/lambda-can-only-be-used-with-functional-interface). Googling the error got me that and a handful of other things that looked useful. – Becuzz Sep 01 '15 at 15:51
  • I'm not sure what code would be useful to share, but I'm open to suggestion. I understand that functional interfaces can only have one method, but I'm not sure why this is even coming up for my code. There are no user-defined interfaces in the project, functional or not. Plus, the error seems to be saying there is a problem with Path, not one of my classes. Elsewhere in the project, I do import java.nio.file.Path, which is a non-functional interface. Why would the compiler suddenly think it should be otherwise? – Jess Sep 01 '15 at 15:57
  • 1
    Try to compile your source files using the command line `javac` to find out, whether there’s something wrong inside the Netbeans configuration. – Holger Sep 01 '15 at 16:00

1 Answers1

3

Functional interface has a "single abstract method" (SAM). Maybe you are attempting to use Path in a way that requires it be SAM (e.g. as a lambda), but Path isn't SAM. https://dzone.com/articles/introduction-functional-1

Jonathan Crosmer
  • 786
  • 5
  • 19
  • That seems like a good thought. I don't really see anything like that though. If I comment out everything in the file that Netbeans believes the error is in, then I can build the project without problems. So, somehow, this class that doesn't even use Path is resulting in this compilation error. – Jess Sep 01 '15 at 16:14
  • I *think* this is what was causing the problem. Once I identified that Netbeans was right about which class had the error, I looked more closely at that code. There was something odd in the constructor. One of the arguments to the constructor wasn't hooking up correctly to its mention inside a lambda expression. I noticed there was a missing argument to something else in that lambda expression. When I fixed that, the constructor argument connected to the mention of the argument in the lambda expression and the error went away. – Jess Sep 01 '15 at 16:19
  • Still, to close the loop, the lambda expression in question doesn't use Path anywhere, so I still don't understand why the error was showing up in this particular way. Weird. – Jess Sep 01 '15 at 16:20
  • Hard to say without seeing any code, but my guess would be that for some reason the compiler was inferring that your lambda was used as a Path. In order to make functional programming not be onerous (with redundant types everywhere), Java 8 adds type inference in more places, which can occasionally cause a confusing compile error if you make a mistake. – Jonathan Crosmer Sep 01 '15 at 20:32