Bound Mismatch

Sorry in advance because this is surely a really simple problem you see all the time, but whats wrong with this code...public abstract class DRENode {
    public <T extends Collection<? extends DRENode>> T getAllNodes(T c){
         c.add(this);
         return c;
}The error I'm getting is at the c.add line and it says..."Bound mismatch: The method add(? extends DRENode) of type T is not applicable for the arguments (DRENode). The wildcard parameter ? extends DRENode has no lower bound, and may actually be more restrictive than argument DRENode"

What you're saying is that your collection is of some fixed type that extends DRENode. In particular, it might be a collection of a subtype of DRENode, in which case being allowed to put a DRENode in to it would be incorrect.
I suspect you want <? super DRENode>, or maybe even just <DRENode>

Similar Messages

  • Bound mismatch error

    Hi everyone
    I am developing a 3d engine using java and at this point I need to create an edge table for my rendering purposes. here is my problem:
    I Have an ArrayList of Edge pointed to by a List reference like this:
    List<Edge> list = new ArrayList<Edge>();
    and i want to use the Collections.sort(list) to sort this list, but when i try to sort the list my editor shows an error saying:
    Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the
    arguments (List<Edge>). The inferred type Edge is not a valid substitute for the bounded
    parameter <T extends Comparable<? super T>>
    here is my Edge class:
    public abstract class Edge implements Comparable{
         protected double ymax;
         protected double ymin;
         protected double xval;
         protected double mInverse;
         public abstract int compareTo(Object e);
    could anyone please tell me what is the problem here?
    thanks
    sina
    Edited by: sina_izad on Dec 27, 2007 10:11 AM

    Well, like the error message suggests, you need this:public abstract class Edge implements Comparable<Edge>and consequently this:public abstract int compareTo(Edge e);

  • Intricate generics problem

    I have a problem with this code:
    interface Test<T extends Number> {
        public <U> void test(Test<? super U> t, U value);
    }Sun javac 1.6.0_13 accepts it, Eclipse 3.5.1 says:
    "Bound mismatch: The type ? super U is not a valid substitute for the bounded parameter <T extends
    Number> of the type Test<T>". This, at first glance, seems a valid complaint, but on the other hand, how could there ever be a parameterized Test that is not of type <T extends Number> passed as a parameter?
    So, my question is: which one is right? I suspect javac is correct and Eclipse has a bug here, but why? Is it correct of the compiler to rely on the implicit bounding of all Test objects as defined in the interface declaration to <T extends Number>, or is it correct (as Eclipse's syntax checker does) to demand an explicit one?
    Could anyone point me to the appropriate Java reference paragraph covering this situation?
    Anyway, either javac or Eclipse has a bug here obviously, I'm just not sure which one!
    Edited by: marhaus on Nov 4, 2009 6:50 AM

    What confuses me is why Javac might accept that, but neither accepts this:
    public interface Test<T extends Number> {
        public <V> void test(Test<V> t);
    }If this doesn't work, I certainly understand why your example doesn't. I think it probably has to do with how the V (or U in your case) is inferred, and that's why they differ.
    For instance, consider this:
       public static void main(String[] args) {
          String s = getSomeObj();
       private static <V> V getSomeObj() {
          return null;
       }Here, V is inferred by the fact that you're assigning to a String. Everything compiles fine. But now consider if we changed getSomeObj:
       private static <V> V getSomeObj(SomeClass<V> sc) {
          return null;
       private static class SomeClass<V extends Number> {
       }This now doesn't compile, perhaps because the bounds of V are not determined at all by how it is used in the arguments. Similarly,
       private static <V> SomeClass<V> getSomeObj() {
       Won't compile, for the same reason. So it seems like neither goes into the decision-making of the bounds of V. It seems like the bounds are decided completely by what's in the <V> part.
    If this is all for learning, by all means continue. I would almost say that it's a javac anomaly. But if it's an actual problem, I would say just do this instead:
        public <V extends Number, U extends V> void test(Test<V> t, U value);

  • Generic Collections and inheritance

    Hi all
    I have the following code:
    public class GenericTest {
         public static <T,C extends Collection<T>> C process(C collection){
              // do some stuff (using T)
              return collection; // just to make a complete methode
         public void main(String[] args){
              List<String> l1 = new ArrayList<String>();
              List<String> l2;
              l2 = process(l1); // does not compile
    }Compilation fails with message :
    Bound mismatch: The generic method process(C) of type GenericTest is not applicable for the arguments (List<String>) since the type List<String> is not a valid substitute for the bounded parameter <C extends Collection<T>>     
    My questions:
    why doesn't it work?
    How do I fix it?
    regards
    Spieler

    What Compiler are you using?
    Your code compiles just fine on the JDK 1.5 compiler (once I added an inport for java.util.*)
    If the error message is from your IDE, you should check your IDE's web site for an existing bug, and for assistance.
    This is not the correct forum since your code compiles on the SUN JDK.
    Bruce

  • Sophisticated generics usage?

    Hi!
    I need help and I am not very much into generics.
    With eclipse 3.2.1 I get the following error:
    Bound mismatch: The type ? super E is not a valid substitute for the bounded parameter <E extends ICDE<? extends ICDEHome<E>>> of the type ICDEHome<E>     
    This class forces the error:
    abstract class MyClass<E extends EntityClass<? super H>, H extends HomeClass<? super E>> extends ImplClass<E,H,InterfaceClass> implements OneMoreInterfaceClass (this is not my code I took it over)
    What really makes me wonder is that NetBeans 5 does not complain and compiles the code. Is it valid code? Why is eclipse choking on it?
    Regards

    I get the following errors in eclipse 3.2.1
    Bound mismatch: The type ? super E is not a valid substitute for the bounded parameter <E extends Class_01<? extends Class_02<E>>> of the type Class_02<E>     
    Bound mismatch: The type ? super H is not a valid substitute for the bounded parameter <H extends Class_02<? extends Class_01<H>>> of the type Class_01<H>
    abstract class MyClass<E extends Class_01<? super H>, H
    extends Class_02<? super E>> extends Class_03<E,H,Class_04> implements Class_05{
    public abstract class Class_03<E extends Class_01<? super H>, H extends Class_02<? super E>, P extends Class_06<? extends Class_07>>
    extends Class_08<E,H,P> implements Class_05{
    public abstract class Class_08<E extends Class_09<? super H>, H extends Class_10<? super E>, P extends Class_06<? extends Class_07>>
    extends Class_11<E,H,Class_12> implements Class_05{
    public abstract class Class_11<E extends Class_13<? super H,O>, H extends Class_14<? super E,O>, O>
    extends Class_15<E,H,O> implements Class_05{
    public abstract class Class_15 <E extends Class_13<? super H,O>, H extends Class_14<? super E,O>, O> extends Class_16{
    public abstract class Class_16{
    public interface Class_01<H extends Class_02<? extends Class_01>> extends Class_09<H>{
    public interface Class_02<E extends Class_01<? extends Class_02>> extends Class_10<E>{
    public interface Class_10<E extends Class_09<? extends Class_10>> extends Class_14<E,Class_12>{
    public interface Class_09<H extends Class_10<? extends Class_09>> extends Class_13<H,Class_12>, Class_17{
    public interface Class_13<H extends Class_14<?, O>, O>{
    public interface Class_14<E extends Class_13<?, O>, O>{
    public final class Class_12{
    public interface Class_17{
    public interface Class_04 extends Class_06<Class_18>, Class_19{
    public interface Class_18 extends Class_07<Class_04>{
    public interface Class_19{
    public interface Class_05{
    }I hope I got all the classes involved ...
    Message was edited by: JvW

  • Searching Sorted List

    I'm trying to write a method that searches a sorted list using compareTo() rather than equals(), as follows:
    public static <T extends Comparable<T>> T find(final T instance, final List<T> list) {
       T result = null;
       for (final T entry : list) {
          final int comparison = entry.compareTo(instance);
         if (comparison < 0) {
            continue;
            if (comparison > 0) {
               break;
         result = entry;
         break;
       return result;
    }I have two classes, A & B:
    Class A implements Comparable<A>
    Class B extends AI don't understand why this won't compile:
    List<B> list;
    B item;
    final B result = find(item, list);The error message reads like this:
    Bound mismatch: The generic method find(T, List<T>) ... is not applicable for the arguments (B, List<B>).
    The inferred type B is not a valid substitute for the bounded parameter <T extends Comparable<T>>Is this just a syntax error on my part, or is not possible to do what I'm attempting? Or is there a standard Collections class that already does this? I looked at Collections.binarySearch(), but I need to use compareTo() rather than equals() to determine a match.

    It looks like you are writing a linear search version of the Collections.binarySearch.
    [http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#binarySearch%28java.util.List,%20T%29]
    You will note it has been declared
    public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)

  • Gneric error intepretation

    I have the following heirarchy
    public interface IElement<K> extends Comparable<K>, Serializable
    public interface IDef<T> extends Comparable<T>, Serializable
    public final class SourceSystemElement implements IElement<SourceSystemElement>
    abstract public class abc<T extends IDef<T>>
    abstract public class Abc2<T extends IElement<T>> implements IDef<Abc2<T>>      
    public class abc3 extends Abc2<SourceSystemElement>
    public final class abc4 extends abc<abc3>
    Gives the following error foir abc4
    Bound mismatch: The type abc3 is not a valid substitute for the bounded parameter <T extends IDef<T>> of the type abc<T>
    Can't figure out why. Following through this logic I figure it should be doing it.But for some reason it is not accepting it. I am missing something here. Since abc2 implements IDef<Abc2<T>> ABC<ABC3> i RECKON SHOULD BE OK.

    As far as I understand:
    abc<T extends IDef<T>>
    therefore
    abc<abc3> should be abc<abc3 extends IDef<abc3>>
    and we have
    abc3 extends Abc2<SourceSystemElement>
    Abc2<T extends IElement<T>> implements IDef<Abc2<T>>
    therefore it's
    Abc2<SourceSystemElement extends IElement<SourceSystemElement>> implements IDef<Abc2<SourceSystemElement>>
    IDef<Abc2<SourceSystemElement>> not IDef<abc3>>

  • How to declare java.lang.Class variable that can be used in Enum.valueOf(.)

    As per subject -- we have a need to declare variable in class that will hold enum's Class object. At some point we want to use this variable as a Class argument for Enum.valueOf(.) which has the following signature:
    public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) {..}So far we cannot come up with a workable solution.
    private final Class<? extends Enum<?>> propertyClass;doesn't work (apparently because second ? should in fact be the same as first ? -- which works with T for class/method templates, but cannot be used for variables). Error message (in Eclipse) is:
    Bound mismatch: The generic method valueOf(Class<T>, String) of type Enum<E> is not applicable for the arguments (Class<capture#13-of ? extends Enum<?>>, String). The inferred type capture#13-of ? extends Enum<?> is not a valid substitute for the bounded parameter <T extends Enum<T>>
    So what would be the proper way to declare the variable?
    Thanks in advance!

    the enum.valueof method is a little weird. they implemented it so it is nice and "safe". if you don't care about that safety, you can probably work with raw types for that bit of code and get the call to go through. you just won't have any type safety on the result.

  • Simple method but hard with generics: compareTo()

    I adjusted my getMaximum() method to generics to avoid the compiler warnings:
        static public Comparable<Comparable> getMaximum(Comparable<Comparable> comparable1, Comparable<Comparable> comparable2) {
            Comparable<Comparable> result = (comparable1 != null) ? comparable1 : comparable2; //init
            if (comparable1 != null && comparable2 != null) {
                int c = comparable1.compareTo(comparable2);
                result = (c > 0) ? comparable1 : comparable2; //comparable1 > comparable2?
            return result;
        }//getMaximum()But now, my other classes that call this method e.g. with 2 dates or with 2 numbers don't compile anymore. I tried
        static public Comparable<? extends Comparable> getMaximum(Comparable<? extends Comparable> comparable1, Comparable<? extends Comparable> comparable2) {
            Comparable<? extends Comparable> result = comparable1; //default
            if (comparable1 != null && comparable2 != null) {
                int c = comparable1.compareTo(comparable2); //compile error
                if (c <= 0) {
                    result = comparable2;
            } else if (comparable1 == null) {
                result = comparable2;
            return result;
        }//getMaximum()but this code doesn't compile:
    Bound mismatch: The method compareTo(? extends Comparable) of type
    Comparable<? extends Comparable> is not applicable for the arguments
    (Comparable<? extends Comparable>). The wildcard parameter ? extends
    Comparable has no lower bound, and may actually be more restrictive than argument
    Comparable<? extends Comparable>

    Whilst I know this thread is quite old, I found it whilst trying to solve the same problem.
    For reference, the following code can be used to overcome the 1.5 compiler warning for the COMPARABLE_COMAPRATOR function in Sun's TableSorter.java.
    public static final Comparator<Object> COMPARABLE_COMAPRATOR = new Comparator<Object>()
        public int compare(Object o1, Object o2)
          int retval = -1;
          Class o1class = o1.getClass();
          Class o2class = o2.getClass();
          if (o1class.equals(o2class))
            try
              Method method = o1class.getDeclaredMethod("compareTo", new Class[]
                                                        {o2class});
              try
                Integer answer = (Integer) method.invoke(o1, o2);
                retval = answer.intValue();
              catch (Exception e)
                System.out.println("Call to method failed.");
            catch (NoSuchMethodException e)
              System.out.println("No compareTo() method.");
          return retval;
      };Hope this helps someone.
    Regards,
    Ray

  • Recursive Generics and Wildcards?

    Does anyone here know how to properly use type wildcards with recursive type definitions?
    interface Foo<S, T extends Foo<S, T>> {
        S getS();
        void mergeWith(T other);
    class FooImpl implements Foo<String, FooImpl> {
        public void mergeWith(FooImpl other) {
        public String getS() {
            return "";
    class Bar<T extends Foo<?, T>> {
        public static void main(String[] args) {
            new Bar<Foo>();
            new Bar<Foo<?, ?>>();
            new Bar<Foo<?, ? extends Foo<?, ?>>>();
    }None of these compile. They all give bounds mismatch errors similar to:
    Bound mismatch: The type Foo is not a valid substitute for the bounded parameter <T extends Foo<?,T>> of the type Bar<T>
    I'm assuming the problem is that the compiler can't realize that I intend the wildcards to refer to the same type. This seems to present a problem for Types that use generics recursively. Is there a way to use a named wildcard type in a declaration?
    Is there another way that I should go about instantiating a Bar?

    Thanks for the answers. I'll have to come up with a better example of where the recursive types cause problems in my application.
    In the meantime (guess I'm hijacking my own thread here a bit) how about instantiating an anonymous subclass of Foo()
    I assume since the subclass is anonymous, this is impossible since the recursive type cannot refer back to the anonymous class:
    Foo<String, ?> foo = new Foo<String, <WHAT TO PUT HERE?>>() {
                public String getS() {
                    // TODO Auto-generated method stub
                    throw new RuntimeException("Method not implemented!");
                public void mergeWith(<WHAT TO PUT HERE?> other) {
                    // TODO Auto-generated method stub
                    throw new RuntimeException("Method not implemented!");
            };

  • Difficulty Making Generic Class

    Hi. I've been attempting to make the following method generic:
    public static <E extends Comparable<E>> E readNumber(String prompt, String prompt2) {
            String input;
            char type; // necessary in order to select the correct methods and appease Eclipse
            int intRes;
            double dRes;
            type = readChar(prompt2);
            if (type == 'i' || type == 'I') {
                    try {
                            input = readString(prompt);
                            intRes = Integer.parseInt(input);
                    catch (Exception e) {
                            intRes = -1;
            } else if (type == 'd' || type == 'D') {
                    try {
                            input = readString(prompt);
                            dRes = Double.parseDouble(input);
                    catch (Exception e) {
                            dRes = -1;
            if (type == 'i' || type == 'I')
                    return intRes;
            else if (type == 'd' || type == 'D')
                    return dRes;
            return 0;
    }The readString method is as follows:
    public static String readString(String prompt) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String input = "";
            boolean done = false;
            while (!done) {
                    try {
                            if (prompt != null)
                                    System.out.print(prompt);
                            input = br.readLine();
                            if (input.length() > 0)
                                    done = true;
                    catch (Exception e) {
                            input = null;
            return input;
    }readChar is as follows:
    public static char readChar(String prompt) {
            String input;
            char result;
            try {
                    input = readString(prompt);
                    result = input.charAt(0);
            catch (Exception e) {
                    result = '0';
            return result;
    }All appropriate libraries (ie java.io.*) are included.
    Eclipses refuses to allow me to return either intRes or dRes (or, indeed, 0), since they can't be cast into E. If I set intRes and dRes as E, then I can't use the Integer and Double methods.
    Can this be made generic, or will I have to keep creating new methods for each primitive type?
    Edited by: KBKarma on May 11, 2008 6:06 AM
    ... Didn't notice the Preview tab. So sorry.

    KBKarma wrote:
    However, in the main class, I initialised an integer variable one with the line readNumber("Enter an integer: " , 'i').intValue(). Eclipse brought up the following error:
    "Bound mismatch: The generic method readNumber(String, char) of type Input is not applicable for
    the arguments (String, char). The inferred type Comparable<Comparable<E>> is not a valid
    substitute for the bounded parameter <E extends Comparable<E>>"So I assume, the code for readNumber above is incorrect, as the invoked method here takes a char as second parameter and no String.
    The main problem is that the compiler tries to infer the type of the return value from whatever its context is, and it obiously fails to do so. It's difficult to say why, without seeing the actual call including variable type definitions etc.

  • Another Mysterious Compiler Error

    I get a warning and a compiler error on the code below...
    public class ClassA {}
    public interface InterfaceB {}
    public class ClassAB extends ClassA implements InterfaceB {}
    public interface HasAB
        <T extends ClassA & InterfaceB> T getAB();
    public class ClassHasAB implements HasAB
        public ClassAB getAB() {return new ClassAB();}
    public class Foo
        public void foo(ClassA a) { /* do something */ }
        public void bar()
            HasAB hasAB = new ClassHasAB();
            foo(hasAB.getAB());
    }The warning is on this line:
    public ClassAB getAB() {return new ClassAB();}
    Type safety: The return type ClassAB for getAB() from the type ClassHasAB needs unchecked conversion to conform to T from the type HasAB
    The error is on this line:
    foo(hasAB.getAB());
    Bound mismatch: The generic method getAB() of type HasAB is not applicable for the arguments (). The inferred type ClassA is not a valid substitute for the bounded parameter <T extends ClassA & InterfaceB>
    Anyone able to explain why?
    I've read Angelika Langer's FAQ, but perhaps I missed the relevant section? If so, my apologies. Feel free to point it out to me.
    Note: I'm using jdk 1.6.

    In case you're wondering why I'm even doing this, here's my use case...
    I'm using the Google Web Toolkit (GWT). It has a class called Widget, which is the ancestor class of a whole hierarchy of other classes. GWT also has an interface called SourcesKeyboardEvents, allowing you to attach listeners. Some of the sub-classes of Widget (but not all) implement it. Widget itself does not implement SourcesKeyboardEvents.
    Several of the GWT classes have methods which take Widget arguments. For example, the FlexTable class has a setWidget(int row, int col, Widget widget) method.
    Now, I have created a DataRow interface with several getter methods with signatures such as...
    <T extends Widget & SourcesKeyboardEvents> T getNameWidget();
    <T extends Widget & SourcesKeyboardEvents> T getDobWidget();
    <T extends Widget & SourcesKeyboardEvents> T getSexWidget();Elsewhere, I want to take a collection of these DataRow objects, and pull widgets from their getters and throw them into a FlexTable (using setWidget, which expects a Widget object). But I also want to add KeyboardListeners to each of them (using their addKeyboardListener method, which all SourcesKeyboardEvents objects must have).
    So, I guess my question is... If the compiler won't let me do as above, what type should my DataRow getter methods return? I need to be able to treat the objects as both Widgets and SourcesKeyboardEvents objects. Sorry if I've missed something obvious. ;)
    Perhaps you can think of a better design. I'd be interested. Nevertheless, I'm also interested in the theory / bug behind the compiler error in the original post.

  • Nested Generics

    I'm trying out this generics example and I cannot get it to compile.
    Here's the error msg:
    Bound mismatch: The type SubB<SubA> is not a valid substitute for the bounded parameter <T extends B<? super A>> of the type X<T>
    class A {
         class SubA extends A {
         class B<T extends A> {
         class SubB<T extends A> extends B<T> {
         class X<T extends B<? super A>> {
    new X<SubB<SubA>>(); // compilation error

    SubB extends B... check. SubA is a superclass of A... nope.
    If your goal is simply to write some code which compiles (and I don't see any other possible purpose for that code) then try declaring X like this:
    class X<T extends B<? extends A>>

  • Doco/functionality mismatch: Adding a Bounded Task Flow to a Page

    Hi JDev team
    With JDev 11g TP4 and the latest published ADF Guide in hand, under section 14.3 "Adding a Bounded Task Flow to a Page", subsection "To add a bounded task flow containing pages to a JSF page:" it notes that when you drag n drop a bounded task flow from the App Navigator into a JSF page you get the choice of creating a button or link. I don't get this option, instead I can create a region or a dynamic region in the drag n drop submenu.
    Cheers,
    CM.

    Chris,
    note that it says "containing pages", not "containing page fragments". If your taskflow contains page fragments "jsff" then the choice is a region, if it is pages (jspx) then the choice is button or link to launch the taskflow.
    Let me know if this still doesn't work for you
    Frank

  • Error related to namespace mismatch in BPEL

    Hi All
    We are facing an error related to NameSpace of a variable in BPEL process.
    We are sending a SOAP XML request to a third-party web service and fetching the XML response in a variable. There is a mismatch in the namespace of the variables. The xml response is having ‘ns1:’ while the variable created in BPEL process is having ‘ns23:’. Thus the process is giving the following error when initiated:
    <part name="code" >
    <code>Server.userException</code>
    </part>
    <part name="summary" >
    <summary>when invoking endpointAddress 'https://cservice1.hrapply.com/WS/erp/ERPService', org.xml.sax.SAXParseException: The prefix "ns1" for element "ns1:newHireResponse" is not bound.</summary>
    </part>
    Complete error message is attached. We tried to rename the variable by changing the namespace in <process>.bpel file but that did not solve the issue. We have also tried to create a new process where the namespace given to the new output variable is ns1: But still he error persists.
    Any pointer/suggestion to solve this issue is highly appreciated.
    PS: We are using BPEL: 10.1.2.0.2

    java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServletYou don't know how to solve class not found errors? That's incredibly basic Java classpath stuff that you not only should know, you MUST know how to handle before you can even think about touching a web environment. You are lacking some really basic core Java knowledge here. Are you perhaps trying to learn Java by diving right into a web application? If so - you can give up now, you have to start from the beginning and that is learn Java. Not only the language, the platform and the tools too.

Maybe you are looking for

  • How do I combine several short movies into one large project?

    You’ll often find a longer project much easier to work with if you work on it in shorter pieces. Doing this can minimize system lugging and maximize program responsiveness as well as reduce the likelihood that you’ll run into problems when you try to

  • What is causing freeze on video plybk?

    i am trying to view the proj. rvr. pltf. and i would like to know what is causing the freeze on video plybk, is it in the programming and/or designing.  if, corrected it will improve the presentation as well, quality.  an inquiring mind would like to

  • Initial testing Multisim + ULTIboard Buglist

    I have been testing Multisim and ULTIboard (V10.0.144) for 1 day to see whether it really works and is usable. I'm still working with Ulticap and Ultiboard V5.7x and consider changing over. I have found the following problems in the process of Schema

  • Installing Windows 7 from USB flash (Macbook Air mid 2012)

    Hello, I read some famous instructions, but they are not for Macbook air mid 2012 as there are some missing options in their Bootcamp Assistant and this on macbook air 2012. Btw, they suggested some rEFIt program which supports installing windows fro

  • How to execute a method on mouse move event on an Image

    Hi All, I want to execute a method on mouse move event or mouse click event to an image. how can i achieve this? TIA, Vishal