1

So i have constructor that looks so:

public Group(Entry<String, List<String>> rawGroup) {
    permission=rawGroup.getKey();

    List<String> params = rawGroup.getValue();

    limits = Integer.parseInt(params.get(0));
    int a = Integer.parseInt(params.get(1));
    int b = Integer.parseInt(params.get(2));
    s1 = Math.min(a, b);
    s2 = Math.max(a, b);
}

And "List params = rawGroup.getValue();" makes that:

java.lang.ClassException: java.lang.String cannot be cast to java.util.List

I can't understand why this is happening, getValue() can't return String because it's not String

UPDATE: Entry is a part of EntrySet that returns Map

UPDATE2: so here's code that uses that constructor -

    Map<String, List<String>> rawGroups = (Map) holder.config.getConfigurationSection(HEADING).getValues(true);
    for (Entry<String, List<String>> rawGroup : rawGroups.entrySet()) {
        groups.add(new Group(rawGroup));
    }
5
  • What is Entry? What does getValue return? Commented May 17, 2015 at 15:30
  • It's impossible to help you without knowing what the Entry class is and how its getValue function works. Entry isn't a standard JDK or JEE class or interface, so... Commented May 17, 2015 at 15:30
  • @T.J.Crowder There's ofcourse Map.Entry, which is a standard JDK class. But we don't know if that what the OP means with Entry. Commented May 17, 2015 at 15:35
  • 1
    You are probably using unchecked casting or raw types somewhere and now you have heap pollution. Your Entry has something in it that's not supposed to be there. We can't help you from this short snippet because the problem is not here, it's somewhere else, where incorrect values get put in the map somehow. Or possibly you get a Map from somewhere, and have misunderstood what it is supposed to store. Commented May 17, 2015 at 15:35
  • 2
    Yes you have heap pollution. You need to check the documentation for holder.config.getConfigurationSection(HEADING).getValues(true); and find out what it actually returns. It's not a Map<String, List<String>>. If it's code that you wrote and there is no documentation, you need to examine that code. Commented May 17, 2015 at 15:38

1 Answer 1

2

The key line is here:

Map<String, List<String>> rawGroups = (Map) holder.config.getConfigurationSection(HEADING).getValues(true);

You've assumed that what holder.config.getConfigurationSection(HEADING).getValues(true) returns is a Map<String, List<String>>, and told the compiler to make that assumption as well. Clearly that's not the case, because when you try to use it that way, it fails.

You need to find out what holder.config.getConfigurationSection(HEADING).getValues(true) is really returning, and use that.

Here's a simple demonstration of the same basic concept (live copy):

public static void main (String[] args)
{
    Map<String, List<String>> m = (Map)getMap();
    try {
        System.out.println(m.get("entry").get(0)); // Fails here
    }
    catch (Exception e) {
        System.out.println("Failed: " + e.getMessage());
        e.printStackTrace(System.out);
    }
}
static Object getMap() {
    Map m = new HashMap();
    List l = new LinkedList();
    l.add(42);
    m.put("entry", l);
    return m;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Or perhaps if the Map is filled by code implemented by the OP, they need verify whether they are filling it correctly.
so object that returns holder.config.getConfigurationSection(HEADING).getValues(true) is java.util.LinkedHashMap
@XsergeiX: Sure, but a LinkedHashMap of what?
Ohh, now i get understand what happend, i got Map<String, String> because of stupid mistake in config. Sorry for trouble.
I am sure we're all glad to help. This kind of error is hard to debug unless you know what to look for. That's why you need to be extra careful when writing code like this (unchecked conversions, raw types, etc...) and avoid it if at all possible.

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.