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(); }}
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();
}
}
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.
@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);
}
}
}
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.
thank you
I am getting an error Type arguments provided for a non-parameterized type: Iterable
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?