Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Arrays Gotchas and Wins Array Usage in Method Declarations

Error message: attempted to call method pickLunchSpot(String...) which cannot be invoked until variable spot is declared

Hi, I'm attempting to call the method pickLunchSpot in jshell, but I get the error message you see in the title. I've copied the code from the video, but for some reason mine doesn't work. Can any of you spot an error in my code? Here is the code:

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

// Not important
String[] friends = {
  "Treasure",
  "Ben",
  "Alena",
  "Pasan",
  "Craig"
};
// The method I'm attempting to call
public String pickLunchSpot(String... spots) { 
  System.out.printf("Randomly picking %d lunch spots. %n",
                   spots.length);
  Random random = new Random();
  return spot[random.nextInt(spots.length)];
}

3 Answers

Hi Sondre. It looks like you just have a small typo in your return statement. It should be spots (with an 's'), not spot.

return spots[random.nextInt(spots.length)];

The system is looking for a spot variable that doesn't exist.

Hope that helps :)

Thank you! :)

You're welcome! :)

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Hi Sondre Dahl

I cannot see any problem with your code.

Try supplying a String array when you call pickLunchSpot() method. for example

pickLunchSpot("Hilton", "MacDonald", "Pizza");

How to set this up in intelliJ?