Results 1 to 5 of 5
  1. #1
    JeremyReeder is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Casting failed when iterating over a Set

    I'm working on a targeting system and having a problem when I iterate over a Set of Bogeys.

    Here's the code:

    public class BogeySet<Bogey> extends TreeSet<Bogey> {
    public Bogey get(String name) {
    for (Bogey bogey : this) {
    if (bogey.getName().equals(name)) {
    return bogey;
    break;
    }
    }
    return null;
    }
    }


    And here's the error:

    BogeySet.java:7: cannot find symbol
    symbol : method getName()
    location: class java.lang.Object
    if (bogey.getName().equals(name)) {

    1 error


    It seems to me that I am clearly specifying that the Objects within the set are Bogeys, so why is Java considering them merely Objects and complaining?

    By the way, there is a reason why I'm using a Set instead of a Map. For most purposes within my program I want to consider this a NavigableSet, and it seemed easier to simply add a getter to my Set than to adapt a Map to sort by value and get the values throughout the rest of my code.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    are you meaning
    class BogeySet<E extends Bogey> :confused:

    and return bogey; break; makes no sense. Delete the break; !

  3. #3
    JeremyReeder is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Question E extends Bogey?

    BogeySet extends TreeSet, and BogeySets only contain Bogeys.

    As for "<E extends Bogey>", I don't know what you mean. That makes no sense to me or to my compiler.

    You are correct, of course, that my break command would never execute. So I removed it.

    But what's causing this "cannot find symbol" error? Bogey has a getName() method and it would be executed if Java considered bogey to be a Bogey. But Java is considering bogey to be simply an Object, which has no getName() method. Very strange.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    If I'm not mistaken that should be
    Java Code:
    public class BogeySet extends TreeSet<Bogey> {
    db

  5. #5
    JeremyReeder is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Talking Thanks

    Thank you. Since BogeySets never need to contain anything other than Bogeys, changing "BogeySet<Bogey>" to "Bogey" throughout my code resolved the problem.

Similar Threads

  1. Iterating over a map of objects
    By javaaz in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-08-2010, 10:29 PM
  2. Iterating through ArrayList using a GUI
    By DC200 in forum New To Java
    Replies: 0
    Last Post: 08-02-2009, 01:24 AM
  3. Iterating through a HashSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:34 PM
  4. Iterating through ArrayList
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:50 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •