Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » type mismatch: cannot convert from element type object(Eclipse doesn't see method signature of method from other class)
icon9.gif  type mismatch: cannot convert from element type object [message #629604] Tue, 28 September 2010 16:59 Go to next message
Eclipse UserFriend
Eclipse Helios is telling me I have an error here, but the code does compile fine. What is baffling is that the code where it claims I have an Object, the method is declared to return a Collection<Context>. Here's a snippet:

for (Context c : Context.getContexts()) {
    updateCount += c.validateModel(update);
}


The method Context.getContexts() is declared, in another class, as:

public static synchronized Collection<Context> getContexts() {
    ....
    return Collections.unmodifiableCollection(new ArrayList<Context>(contextMapByID.values()));
}


In my compiler preferences, Java > Compiler > Errors/Warnings > Generic Types, the "Unchecked generic type operation" is set to "Warning" (as are the other two items). But I'm not sure how that would even apply anyway. I've tried forcing Eclipse to rebuild the project to no avail.

I'm confused as to how Eclipse concludes that that method is returning a Collection of Object given the call signature. Any clues on what settings I need to get Eclipse to see this correctly?

TIA,

roland
Re: type mismatch: cannot convert from element type object [message #632473 is a reply to message #629604] Wed, 13 October 2010 01:58 Go to previous messageGo to next message
Eclipse UserFriend
Roland Roberts wrote:
> Eclipse Helios is telling me I have an error here, but the code does
> compile fine. What is baffling is that the code where it claims I have
> an Object, the method is declared to return a Collection<Context>.
> Here's a snippet:
>
> for (Context c : Context.getContexts()) {
> updateCount += c.validateModel(update);
> }
>
> The method Context.getContexts() is declared, in another class, as:
>
> public static synchronized Collection<Context> getContexts() {
> ....
> return Collections.unmodifiableCollection(new
> ArrayList<Context>(contextMapByID.values()));
> }
>
> In my compiler preferences, Java > Compiler > Errors/Warnings > Generic
> Types, the "Unchecked generic type operation" is set to "Warning" (as
> are the other two items). But I'm not sure how that would even apply
> anyway. I've tried forcing Eclipse to rebuild the project to no avail.
> I'm confused as to how Eclipse concludes that that method is returning a
> Collection of Object given the call signature. Any clues on what
> settings I need to get Eclipse to see this correctly?
>
> TIA,
>
> roland

Exactly what is the error you are getting? And what are your compiler
compliance settings (that is, are you compiling for 1.5, 1.6, ...?)

Can you reproduce the problem in a small but complete code snippet that you
could post here?

What do you mean by "Eclipse Helios is telling me I have an error here, but the
code does compile file" - do you mean it compiles fine at the command line with
javac? What version of javac?

Are you certain that the declaration of Context that is being imported into the
first class is the declaration you want? (There are a lot of classes named
Context, in different packages...).
Re: type mismatch: cannot convert from element type object [message #636201 is a reply to message #632473] Fri, 29 October 2010 11:44 Go to previous messageGo to next message
Eclipse UserFriend
Okay, here is a complete example showing the "error".

A.java:
public class A {
    public static void main(String[] args) {
        for (String a : B.getAll()) {
            System.out.println(a);
        }
    }
}


B.java:
import java.util.Collection;
import java.util.ArrayList;

public class B {
    static Collection<String> all = new ArrayList<String>();
    public static Collection<String> getAll() {
        return all;
    }
}


Eclipse highlights B.getAll() in the "for" statement with the message "type mismatch: cannot convert from element type Object to String." It also puts a little red X next to the line as if this was an error, yet it does not show up in the "Problems" tab like real compile errors do.

JDK compliance is set to 1.5 (compiler, class, and source).

Java compiler/RT is Sun Java 1.5.0.22; host platform is Fedora 13.

The "error" does not seem to prevent me from doing anything; the code runs (and of course, does nothing since I didn't put anything into the collection). But it's annoying when trying to eliminate all those errors and warnings that this displays as if its an error.
Re: type mismatch: cannot convert from element type object [message #637782 is a reply to message #636201] Mon, 08 November 2010 15:18 Go to previous messageGo to next message
Eclipse UserFriend
Could you please give the exact org.eclipse.jdt.core bundle id from the About dialog > Information> Plugins ?

I tried using latest I-build, and I could not reproduce.

Thanks,
--
Olivier
Re: type mismatch: cannot convert from element type object [message #637788 is a reply to message #637782] Mon, 08 November 2010 15:36 Go to previous messageGo to next message
Eclipse UserFriend
Here's what I have:

Quote:
Eclipse Java Development Tools

Version: 3.6.1.r361_v20100714-0800-7z8XFUSFLFlmgLc5z-Bvrt8-HVkH
Build id: M20100909-0800

(c) Copyright Eclipse contributors and others 2000, 2010. All rights reserved.
Visit http://www.eclipse.org/jdt
Re: type mismatch: cannot convert from element type object [message #638618 is a reply to message #637788] Thu, 11 November 2010 14:26 Go to previous messageGo to next message
Eclipse UserFriend
It works fine for me using the same build.
Could you please zip up your workspace and attach it to a bug report against JDT/Core?

Thanks.
--
Olivier
Re: type mismatch: cannot convert from element type object [message #663965 is a reply to message #629604] Wed, 06 April 2011 20:35 Go to previous messageGo to next message
Eclipse UserFriend
I am seeing this same problem in version 3.6.2.r362_v20110210-9gF78Gs1FrIGnHDHWkEcopoN8AmxeZflGDGKQi
Build id: M20110210-1200

Anything involving a generic gives warnings or errors saying the type is raw and instead of Object I should be passing in a value of type E, but the warnings and errors are spurious.

In addition, the warnings/errors don't appear in the Problems display, only in the source files.

I have blown away my workspace and started over, and it still happens.
Re: type mismatch: cannot convert from element type object [message #663986 is a reply to message #663965] Thu, 07 April 2011 01:50 Go to previous messageGo to next message
Eclipse UserFriend
Can you try the latest 3.7 milestone build? They were some issues that got fixed in 3.7.
Re: type mismatch: cannot convert from element type object [message #664154 is a reply to message #663986] Thu, 07 April 2011 12:16 Go to previous messageGo to next message
Eclipse UserFriend
Installing the latest 64-bit version made the problems go away.
Re: type mismatch: cannot convert from element type object [message #699924 is a reply to message #629604] Fri, 22 July 2011 11:21 Go to previous messageGo to next message
Eclipse UserFriend
Someone on this thread said that the 64-bit version fixed the problem.
I am (still!) using WinXP(SP3) so the 32-bit version is still broken.
I am using the release version:
Eclipse Java EE IDE for Web Developers.
Version: Indigo Release
Build id: 20110615-0604

Is there any setting/config/whatever I can tweak to make this erroneous error go away?

---
Mike Norman
Re: type mismatch: cannot convert from element type object [message #793578 is a reply to message #629604] Wed, 08 February 2012 05:01 Go to previous messageGo to next message
Eclipse UserFriend
Hi,
I signed up just to report that I have the exact same problem as the OP.
I'm using this build:
Version: Indigo Release
Build id: 20110615-0604

The code that offends the editor is:
...
for(String line: file.getFileContents()){
...




public List<String> getFileContents() {
   return fileContents;
}


The call to "file.getFileContents()" is red and underlined, and a mouseover gives the error "Type mismatch: cannot convert from element type Object to String".
Meanwhile, there is no error in the Problems tab, and my project builds perfectly. And, more importantly, the code works! =)

Re: type mismatch: cannot convert from element type object [message #793599 is a reply to message #793578] Wed, 08 February 2012 05:34 Go to previous messageGo to next message
Eclipse UserFriend
Umm.. not able to reproduce the error Sad. Can you reproduce this problem in a smaller testcase?
Re: type mismatch: cannot convert from element type object [message #799716 is a reply to message #793599] Thu, 16 February 2012 03:14 Go to previous messageGo to next message
Eclipse UserFriend
Well, I tried, but if I do the below, it works just fine. The problem only occurs in my larger project, as far as I can tell.

import java.util.ArrayList;
import java.util.List;


public class Test {
	
	private static List<String> getList(){
		return new ArrayList<String>();
	}
	
	public static void main(String [] args){
		for(String line: getList()){
			System.out.println(line);
		}
	}
}



If I use the A & B example by Roland Roberts, I don't get the error either.

My project has this .prefs file:

#Thu Feb 16 09:11:18 CET 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
Re: type mismatch: cannot convert from element type object [message #799941 is a reply to message #799716] Thu, 16 February 2012 08:43 Go to previous messageGo to next message
Eclipse UserFriend
Will it be possible to give your complete workspace or a reproducible test case? If so, please file a bug.
Re: type mismatch: cannot convert from element type object [message #800627 is a reply to message #799941] Fri, 17 February 2012 04:43 Go to previous message
Eclipse UserFriend
I am a beginner, come learn.
Previous Topic:Remote Debug RCP Application
Next Topic:eclipse plugin-IMarkerResolution-How to remove eclipse's own quick fix items from the list
Goto Forum:
  


Current Time: Sat May 17 06:27:40 EDT 2025

Powered by FUDForum. Page generated in 0.07076 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top