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 trialSondre Dahl
10,086 PointsError 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
Lauren Moineau
9,483 PointsHi 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 :)
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 PointsHi 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");
Evander Francis
1,980 PointsHow to set this up in intelliJ?
Sondre Dahl
10,086 PointsSondre Dahl
10,086 PointsThank you! :)
Lauren Moineau
9,483 PointsLauren Moineau
9,483 PointsYou're welcome! :)