-1

I am trying to solve this error, but for some reason, not able to find the issue to fix it. I am using Collection in this code. If anyone can point me out what actually I am missing, I would be a great help.

Collection challenges = null;
        Map challengesMap = new HashMap<String,String>();
        ForgotPasswordManager forgotPwdMgr = new ForgotPasswordManager(platform);
        System.out.println("after ForgotPasswordManager(platform)...before getSecretQuestion()");
        challenges = forgotPwdMgr.getSecretQuestion(userID);
        System.out.println("after getSecretQuestion()...");
        for (Object challenge : challenges) {
            String challengeStr = (String)challenge;
            System.out.println("doGetChallenges()...ChallengeStr = " + challengeStr);
            challengesMap.put(challengeStr.trim(), "");
        }

I am getting this error Can only iterate over an array or an instance of java.lang.Iterable on line: for (Object challenge : challenges)

1

1 Answer 1

3

The Collection type referenced here :

Collection challenges = null;

is not the java.util.Collection interface that extends the Iterable interface.
Otherwise you would have not this error message :

Can only iterate over an array or an instance of java.lang.Iterable

at this line :

for (Object challenge : challenges) {

You are using probably a custom Collection class.

So to solve your problem, make your custom custom Collection class implement Iterable or more simple : use JDK java.util.Collection subclasses.

Sign up to request clarification or add additional context in comments.

6 Comments

can you elaborate little bit more i am little confuse @davidxxx
Ok. Look at the package of the Collection class in your imports. You will see that is not java.util.Collection. So it means that you use your own Collection class.
i am using the import.java.util.Collection when i click on Declared type it brings me to the Collection class @davidxxx
I suppose you have a Collection class in the package of the actual class that declares this code ?
i didn't define the Collection class in my code after importing it could that be the reason because i am only using collection on this piece of the code @davidxxx
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.