Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Local Development Environments Advanced Tooling Teamwork

Error: Java method cannot be applied to given types location - prompter.java


When I try to run the project, I'm getting the following error:

Error:(27, 26) java: method readAllLines in class java.nio.file.Files cannot be applied to given types; required: java.nio.file.Path,java.nio.charset.Charset found: java.nio.file.Path reason: actual and formal argument lists differ in length

I've redownloaded the project twice, still the same error.

4 Answers

Steven Miyakawa
Steven Miyakawa
14,540 Points

You need to make sure the Project language level setting for IntelliJ is at least set to 8. I think by default it sets it to 7. In Java 7, java.nio.file.Files.readAllLines required the charset to be passed in as a 2nd parameter.

File -> Project Structure... -> Project Settings -> Project -> Project Language Level = 8 - Lambdas, Type Annotations etc

To remove the Error please make sure you SDK is at least at 8. Go to file-project structure- Project Settings-project and chose at least 8.

I passed a default Charset to readAllLines in Prompter.java and the code worked. I have not fully understood this solution. It'll really be helpful if anyone can explain why this worked. Here's what I did:

private void loadCensoredWords() { mCensoredWords = new HashSet<String>(); Path file = Paths.get("resources", "censored_words.txt"); List<String> words = null; try { words = Files.readAllLines(file, Charset.defaultCharset()); } catch (IOException e) { System.out.println("Couldn't load censored words"); e.printStackTrace(); } mCensoredWords.addAll(words);

Kabir Gandhiok
Kabir Gandhiok
12,553 Points

I've been getting this error too and have applied the above solution as described by Anand Altekar which fixes it, but I dont understand it. Would be very helpful if someone could explain it, thanks!

Stephen Levy
Stephen Levy
9,844 Points

Using SDK version 9 and encountered the same problem. Anand's solution worked for me.