Skip to main content Join us at TDX in San Francisco or on Salesforce+ on March 5-6 for the Developer Conference for the AI Agent Era. Register now.

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();   }}

 

#Trailhead Challenges

7 answers
  1. Dec 12, 2024, 10:28 PM

    Hi @Edward Dalton,

    • You need to assign strings (In constructor, It should be strings not strList) to strings class variable. Use this keyword.
    • You should replace strings = strList; with this.strings = strings;
    • In constructor remove strings.iterator();

    Code should be like this -

    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();

    }

    }

  2. Dec 17, 2024, 10:20 PM

    I was having this issue, and what solved it for me was I needed to add space around the assignment operator:

    		this.strings = strings;

    The error message was very misleading.

     

    Later on, I got the error that it didn't see the `@IsTest` annotation on the test method and the real problem was extra whitespace in the method declaration.

  3. Dec 16, 2024, 2:19 PM

    @Mark Somers here you go:

     

    @IsTest

    public class MyIterableTest {

    @IsTest

    static void testIterableForLoop(){

    List<String> strings = new List<String>{'Hello','World'};

    for (String str : new MyIterable(strings)) {

    System.debug(str);

    }

    }

    }

  4. Dec 13, 2024, 8:13 AM

    Hi @Edward Dalton,

    Its already mentioned in the Challenge, the full parameter for the constructor - List<String> strings

    Replace List<String> strList with List<String> strings in your constructor parameter and it will solve the issue.

  5. Dec 15, 2024, 7:55 PM

    I'm having a lot of trouble getting the test class for this right, does anyone have a template i could use to dig myself out of the hole?

0/9000