• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

List Interface

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any body give good explantion why in the following code Line 2 is giving me an exception ? I can do the same thing with Line 1 . No problem with Line 1 at all .


[ October 10, 2007: Message edited by: Burkhard Hassel ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is fault the cast

change is line


per this
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Akhil,

welcome to the Ranch!



Tricky problem.

On line 1 Object j = l.get(x); // Line 1
the "get" should return an object of type String, you can nicely assign this to a variable of type Object.
At runtime, the object returned is not a String but the Integer-object 123.
This can also be assigned to an Object without problems.

In line 2 however
System.out.println(l.get(x).getClass().getName());
at runtime you will also get the same 123 Integer object back, and again a String is expected.
But this time the Integer-object is not assigned to an Object variable but to a String variable. This must happen somewhere in the getClass-method. And then the cast fails and you have the exception.
I don't know what happens exactly in the getClass() - method. It is native.

By the way, the getName() should not be the problem, the exception comes also when you delete that part.

Yours,
Bu.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy "Vitor Hugo" !

Thanks for one of your first contributions to this forum and...

Welcome to the Ranch!




Hope you'll enjoy.




Only one small issue: The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp


So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.


I'm posting this because I am one of the moderators of this forum.


Yours,
Bu.
 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic