6

I'm having this error:

Type mismatch: cannot convert from element type Object to String

This is the code in error:

public List<String> customPrefixes(PermissionUser u)
{
    List returnlist = new ArrayList();
    for (String k : u.getAllPermissions().keySet()) {
        List perms = (List)u.getAllPermissions().get(k);
        for (String s : perms) {
            String[] split = s.split(".");
            if ((split.length >= 3) && 
              (split[0].equalsIgnoreCase("plugin")) && 
              (split[1].equalsIgnoreCase("prefix"))) {
                returnlist.add(split[2]);
            }
        }
    }
    return returnlist;
}

3 Answers 3

11

try this :

public List<String> customPrefixes(PermissionUser u)
  {
    List<String> returnlist = new ArrayList<String>();
    for (String k : u.getAllPermissions().keySet()) {
      List<String> perms = (List<String>)(u.getAllPermissions()).get(k);
      for (String s : perms) {
        String[] split = s.split(".");
        if ((split.length >= 3) &&
          (split[0].equalsIgnoreCase("plugin")) &&
          (split[1].equalsIgnoreCase("prefix"))) {
          returnlist.add(split[2]);
        }
      }

    }

    return returnlist;
  }

You were missing "<String>" in the List declaration

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

7 Comments

Are you sure that u.getAllPermissions() returns something that can be cast to List<String>?
PermissionUser seems to be a custom class, so this is up to the composer of this question. He claims that it does
If it can't be, the next error he'll get will be class cast exception
Please rephrase your comment, as i have not downvoted anybody, i don't understand what you mean
@LenaBru Misunderstood you :) Now after you've explained your answer, you get my +1.
|
1

I think you're casting is wrong..

What is u.getAllPermissions().get(k); should return? List of something? if it does so you need to add type of the generic list

List<String> perms = (List<String>)u.getAllPermissions().get(k);

If that doesn't work you can also try to do

for (Object o : perms) {
 String s = o.toString();
 .....
}

Hope that helps.. If not answer my question and it will be easier to help

1 Comment

Casting it to Object works but if we had List<Foo> instead of List<String> we will loose the ability to call methods available on Foo object inside the for loop
0

Another reason for this error can be the way you have initialized your container; as a case in point; you have initialize list2 as below:

List list2 = new ArrayList <Integer>();

instead of

List<Integer> list2 = new ArrayList <Integer>();

So, you are looping over a wrong type of container ( if we can call this a container) as below :

for(Integer x:list2){
            System.out.println(x);
        }

Thus, you need to revise the way you have initialize your container ( here is a list) and there is no problem with your datatype, etc.

Comments

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.