• Post Reply Bookmark Topic Watch Topic
  • New Topic

object to string error  RSS feed

 
Steven Villarreal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have this method in which i'm trying to add a line of code but keep getting this error: incompatible types: java.lang.Object cannot be converted to java.lang.String.


the line of 47 is giving me the error. What would I do to fix this error?
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your ArrayList object is a raw type, that is you haven't told it what it is supposed to be storing, so it defaults to storing Objects.
Have you done Generics yet?

There is one other issue.
Your return on line 47 isn't reachable, which I expect the compiler will complain about when you've fixed the other issue.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 47 is unreachable because you are returning in all of your if-else statements. I don't know why you're getting that specific error message.
 
Steven Villarreal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls how would I fix the array list to make it be able to store what I want?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is because, as you have already been told, you are storing Objects in your List, not Strings. It is therefore quite possible to do this sort of thing:-That means there is no guarantee that what comes out of the List will be a String, so the javac tool goes into pessimistic mode and decides it cannot exclude a wrong type, so it con't compile.
All paths through that method will lead to a return statement not later than line 45, which means line 47 can never be executed, and Java® doesn't permit unreachable code, as somebody told you earlier.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Steven Villarreal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now as you guys were saying the return statement at the end on line 47 is not being reached where would I put that?
 
Steven Villarreal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait nevermind I got it.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already have your returns. Leave 47 out completely.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Villarreal wrote:Now as you guys were saying the return statement at the end on line 47 is not being reached where would I put that?
Nowhere. Delete it. By the time you reach line 45 you have covered all possibilities, so you can't have anything else to write. If that isn't what you intended, then you have done something wrong with the method before line 45.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!