8

I am unable to complete this Trailhead Module: Get Hands-On with an Iterable Variable in For Loops, because I keep getting this error message:

The constructor MyIterable should accept parameter of type List<String>.

Here's my MyIterable Class with its constructor accepting the correct type of parameter:

public class MyIterable implements Iterable<String> { private List<String> strings; public MyIterable(List<String> strList) { strings = strList; strings.iterator(); } public Iterator<String> iterator() { return strings.iterator(); } } 
1
  • 2
    try public MyIterable(List<String> strings) as per the instructions Commented Dec 12, 2024 at 23:09

3 Answers 3

24

Try this one for the constructor and below sample code it should work:

public class MyIterable implements Iterable<String> { private List<String> strings; public MyIterable(List<String> strings) { this.strings = strings; } public Iterator<String> iterator() { return strings.iterator(); } } 

In case you need Test Class:

@IsTest public class MyIterableTest { @IsTest static void testIterableForLoop() { List<String> strings = new List<String>{'Hello', 'World'}; MyIterable iterable = new MyIterable(strings); for (String str : iterable) { System.debug(str); } } } 
5
  • 1
    This happened with me, and it ultimately turned out to be that I had "MyIterable (" instead of "MyIterable(". The name of the variable mattered as well, anything other than "strings" will cause this error to occur. It also fails for another error is you use "return this.strings.iterator();" in the iterator method. I feel like the challenge checker used to be smarter than it is today. Commented Dec 26, 2024 at 20:26
  • Char for char matching for validation is poor IMO, you should not be penalized for actually writing valid and working code instead of copy/paste kludges. This is why so many codebases are hard to work on or don't work well. Esp if it balks on valid whitespace differences that have no impact on function. Commented Jan 3 at 13:30
  • 1
    It failed for me because I had return this.strings.iterator();, and not return strings.iterator(); -_- Commented Jan 4 at 22:59
  • Ugh, I've been developing with SF for almost 20 years and the longer I do it, the more disappointed am I. An exactly correct program fails their test because of inconsequential formatting differences. Commented Jan 7 at 21:03
  • thanks for posting this. i'm still not sure what changed - maybe it was the capitalization on the method name, but it all looked the same as what i had been trying for 20 minutes and this time it passed. the test classes worked fine, the validation just kept failing with "We can't find that the Iterator() method returns the 'strings.iterator()'." Commented Nov 25 at 19:30
0

I ran the test in Setup > Application Test Execution instead of Dev Console .

My test runs in Dev Console did not appear in the Application Test Execution, that was the telltale sign.

I also had a Test start and stop, I'm not sure if that helped. Below are the classes I used

Class:

public class MyIterable implements Iterable<String> { private List<String> strings; public MyIterable(List<String> strings){ this.strings = strings; } public Iterator<String> iterator() { return strings.iterator(); } } 

Test Class:

@IsTest public class MyIterableTest { @IsTest static void testIterableForLoop() { List<String> strings = new List<String>{'Hello', 'World'}; MyIterable iterable = new MyIterable(strings); // Test execution Test.startTest(); for (String str : new MyIterable(strings) ) { System.debug(str); } Test.stopTest(); } } 
-1

Another thing that needs to be noticed is we have to write @IsTest instead of @isTest so that Salesforce can verify our test class.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.