• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Help with compile error "incompatible types: Object cannot be converted to Vampire"

 
Ranch Hand
Posts: 301
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there ,

Can someone help me understand why I'm getting the error " incompatible types: Object cannot be converted to Vampire"  on line 70 ?

I have a Vampire class in the same package as this dungeon class.   The variable "vampireList" is a List of Vampires and I want to iterate over it.  I have done this before on other assignments so I don't understand what the issue is here.

Just FYI: You see the variable "vampireList"  listed twice because I started with it as a local variable on line 61 and when I received the error I created it again at line 16 in an attempt to figure out what maybe going on.



 
Master Rancher
Posts: 195
7
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you specify the type of list you are passing to the method?

 
Lisa Austin
Ranch Hand
Posts: 301
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brecht Geeraerts wrote:What happens if you specify the type of list you are passing to the method?



Well that was it.  I guess I need to be more specific.  Thank You
 
Marshal
Posts: 65466
249
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 59. A Vampire is different from a Vamp

If you cast something, the compiler will implicitly believe you know what you are doing (again ‍) but if you use generics, the compiler goes into fusspot mode and tests every assumption you are making. If it isn't 100% sure that the casts will succeed, it won't let you write the code. The fussier the compiler, the fewer runtime errors you will suffer.
 
Lisa Austin
Ranch Hand
Posts: 301
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Line 59. A Vampire is different from a Vamp

If you cast something, the compiler will implicitly believe you know what you are doing (again ‍) but if you use generics, the compiler goes into fusspot mode and tests every assumption you are making. If it isn't 100% sure that the casts will succeed, it won't let you write the code. The fussier the compiler, the fewer runtime errors you will suffer.



Ah okay.   I get it.  Thank you.  I appreciate the information.    
 
Sheriff
Posts: 13729
228
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lisa Austin wrote:I get it.


It's good that you got something but I don't quite get how/what you "get it".

1. Line 59 is this:
Where on this line are you casting a Vampire to a Vamp (or vice versa)?

Also, I don't even see any reference to a Vamp type anywhere in the code. I was looking at the code on my iPad and line 61 was cut off, showing only ArrayList<Vamp. Could this be what Campbell saw as well and mistakenly assumed was the entire declaration? In fact, the complete code you gave was correct: List><Vampire> = new ArrayList<Vampire>().  There is no casting being done here, so again I'm confused about how you can say "I get it."

2. The real problem, I believe, was already pointed out previously: on line 69, you use a raw List type in your parameter declaration but on line 70, your loop variable is declared as a Vampire which is not compatible with the raw List element type, Object. This will cause a compile-time error with the message you gave in the subject. You're not casting here either, at least not explicitly.
 
Lisa Austin
Ranch Hand
Posts: 301
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Lisa Austin wrote:I get it.


It's good that you got something but I don't quite get how/what you "get it".

1. Line 59 is this:
Where on this line are you casting a Vampire to a Vamp (or vice versa)?

Also, I don't even see any reference to a Vamp type anywhere in the code. I was looking at the code on my iPad and line 61 was cut off, showing only ArrayList<Vamp. Could this be what Campbell saw as well and mistakenly assumed was the entire declaration? In fact, the complete code you gave was correct: List><Vampire> = new ArrayList<Vampire>().  There is no casting being done here, so again I'm confused about how you can say "I get it."

2. The real problem, I believe, was already pointed out previously: on line 69, you use a raw List type in your parameter declaration but on line 70, your loop variable is declared as a Vampire which is not compatible with the raw List element type, Object. This will cause a compile-time error. You're not casting here either, at least not explicitly.



Okay maybe I don't.  I took what he was referring the method name "generateVamps" .  I took it that it should be  "generateVampires" since there isn't any "Vamp" in the code.  

 
Campbell Ritchie
Marshal
Posts: 65466
249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lisa Austin wrote:. . . I took it that it should be  "generateVampires" since there isn't any "Vamp" in the code.  

I am not sure whether vampires or vamps are worse, but I wouldn't feel safe with either around. If you mean createVampire() please write createVampire()
 
Junilu Lacar
Sheriff
Posts: 13729
228
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lisa Austin wrote:Okay maybe I don't.  I took what he was referring the method name "generateVamps" .  I took it that it should be  "generateVampires" since there isn't any "Vamp" in the code.


Well, yeah, generateVampires would probably be a better name than generateVamps() but that wouldn't cause a compile-time error with the message "Incompatible types: Object cannot be converted to Vampire" -- you have to look at the message and think "Where am I trying to convert an Object to a Vampire?" and the only place in your original code is on line 70, but you already fixed that.
 
Lisa Austin
Ranch Hand
Posts: 301
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Lisa Austin wrote:Okay maybe I don't.  I took what he was referring the method name "generateVamps" .  I took it that it should be  "generateVampires" since there isn't any "Vamp" in the code.


Well, yeah, generateVampires would probably be a better name than generateVamps() but that wouldn't cause a compile-time error with the message "Incompatible types: Object cannot be converted to Vampire" -- you have to look at the message and think "Where am I trying to convert an Object to a Vampire?" and the only place in your original code is on line 70, but you already fixed that.



Okay.  I was taking it as additional information since I already fixed the issue in the post above.  I'll ask rather than assume.    I have problems sometimes understanding what someone is trying to convey to me so thank you for pointing it out.    
 
Lisa Austin
Ranch Hand
Posts: 301
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Lisa Austin wrote:. . . I took it that it should be  "generateVampires" since there isn't any "Vamp" in the code.  

I am not sure whether vampires or vamps are worse, but I wouldn't feel safe with either around. If you mean createVampire() please write createVampire()



LOL I wouldn't either. I've updated it to "createVampires" for better clarity.

However in regards to Junilu Lacar's point. Could you clarify your point regarding casting?    
 
Junilu Lacar
Sheriff
Posts: 13729
228
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe Campbell was referring to something like this:

If you write this:

The Java compiler will take your word on the cast on line 3 and assume you know what you're doing.

However, with the kind of code you wrote initially, Java goes into what Campbell calls the "fusspot mode" and applies stricter type-checking rules:

The compiler will see the raw List type and can only assume Object as the element type. Not all Objects are going to be Vampires so the compiler reports that as an incompatible conversion and gives you the error that you were getting.
 
Lisa Austin
Ranch Hand
Posts: 301
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I believe Campbell was referring to something like this:

If you write this:

The Java compiler will take your word on the cast on line 3 and assume you know what you're doing.

However, with the kind of code you wrote initially, Java goes into what Campbell calls the "fusspot mode" and applies stricter type-checking rules:

The compiler will see the raw List type and can only assume Object as the element type. Not all Objects are going to be Vampires so the compiler reports that as an incompatible conversion and gives you the error that you were getting.



Ah okay but I wasn't casting ( hence the "fusspot mode" ) .    Since this method is to only generate/create Vampires , I assume I should have specified the List type ( List <Vampire> vampireList rather than List vampireList ) .  But if it was to create <whatever>  ( if I had more than Vampires, like ...  Werewolves ), then I would have left the parameter as (List characterList) and cast what I wanted?

 
Junilu Lacar
Sheriff
Posts: 13729
228
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since Java introduced generics, it's always a good idea to prefer being more specific about the types you're going to work with. As Campbell said, that just gives the compiler more to work with to check types and avoid potential runtime errors.

With that in mind then,

You wrote:But if it was to create <whatever>  ( if I had more than Vampires, like ...  Werewolves ), then I would have left the parameter as (List characterList) and cast what I wanted


No, assuming you had a hierarchy of Character, Vampire extends Character, Werewolf extends Character, then you can just do this:

You won't need to cast anything here. It's when you want to take the general type and go more specific that you'll need to cast:

This code will compile but it will produce a runtime error (ClassCastException) if the characters list has anything other than Vampire objects in it.
 
Campbell Ritchie
Marshal
Posts: 65466
249
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote: . . . The Java compiler will take your word on the cast on line 3 and assume you know what you're doing. . . . Java goes into what Campbell calls the "fusspot mode" . . .

Yes, that is exactly what I meant. Remember that using generics means you are telling the compiler to go into fusspot mode.

Lucy van Pelt would have said, “fuss‑budget”.
 
Campbell Ritchie
Marshal
Posts: 65466
249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lisa Austin wrote:. . . .  But if it was to create <whatever>  ( if I had more than Vampires, like ...  Werewolves ), . . .

You could consider making the list one of a superclass of Vampire, e.g. List<Monster>. But remember that even if Vampire and Werewolves are subtypes of Monster, a List<Vampire> is not a List<Monster> nor one of its subtypes.

At this point I am thinking all sort of funny thoughts. The Observer's Book of Monsters by Claude Ferociously comes to mind. It was on the Curse of the Were‑Rabbit (Nick Park, Wallace & Gromit).
All the gory details (a bad pun, sorry) about what is and isn't a subtype of something are in the Java™ Tutorials. Or at least they used to be.
Remember, as it says in Effective Java by Joshua Bloch, if your code compiles with no casts, no compiler warnings, and no use of @SuppressWarnings, you can be sure it will run without class cast exceptions. Like when Mum sent you off to school: “Have you got your keys, money, English homework, and watch, Campbell?” If I could answer yes, none of that sort of trouble could befall me.
 
Lisa Austin
Ranch Hand
Posts: 301
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Since Java introduced generics, it's always a good idea to prefer being more specific about the types you're going to work with. As Campbell said, that just gives the compiler more to work with to check types and avoid potential runtime errors.

With that in mind then,

You wrote:But if it was to create <whatever>  ( if I had more than Vampires, like ...  Werewolves ), then I would have left the parameter as (List characterList) and cast what I wanted


No, assuming you had a hierarchy of Character, Vampire extends Character, Werewolf extends Character, then you can just do this:

You won't need to cast anything here. It's when you want to take the general type and go more specific that you'll need to cast:

This code will compile but it will produce a runtime error (ClassCastException) if the characters list has anything other than Vampire objects in it.



Ah Okay I see the difference.   Thank You
 
Lisa Austin
Ranch Hand
Posts: 301
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Lisa Austin wrote:. . . .  But if it was to create <whatever>  ( if I had more than Vampires, like ...  Werewolves ), . . .

You could consider making the list one of a superclass of Vampire, e.g. List<Monster>. But remember that even if Vampire and Werewolves are subtypes of Monster, a List<Vampire> is not a List<Monster> nor one of its subtypes.

At this point I am thinking all sort of funny thoughts. The Observer's Book of Monsters by Claude Ferociously comes to mind. It was on the Curse of the Were‑Rabbit (Nick Park, Wallace & Gromit).
All the gory details (a bad pun, sorry) about what is and isn't a subtype of something are in the Java™ Tutorials. Or at least they used to be.
Remember, as it says in Effective Java by Joshua Bloch, if your code compiles with no casts, no compiler warnings, and no use of @SuppressWarnings, you can be sure it will run without class cast exceptions. Like when Mum sent you off to school: “Have you got your keys, money, English homework, and watch, Campbell?” If I could answer yes, none of that sort of trouble could befall me.



LOL.  Got it.    Thank You
 
Did you just should on me? You should read this tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!