aspose file tools*
The moose likes Beginning Java and the fly likes Help with List class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with List class" Watch "Help with List class" New topic
Author

Help with List class

Terry Chambers
Greenhorn

Joined: May 11, 2009
Posts: 17

I have an XML file which is stored using an object called "Config". When "Config" is instantiated, it reads the XML file and creates "Server" objects.

In my JSP I want to be able to access some "get" methods associated to the "Server" objects so I have created a Config object and then used a method called "getServers" which creates a List of "servers" to get access to the "Server" objects.



I want to be able to work with those "Server" objects by loading them up in objects in the JSP but the "get" method of "List" returns "Object" types and so the above statement returns an error that I cannot convert from "Object" to "Server". How can I convert/cast this properly so that I can access the "get" methods of my Server objects?

Thanks
Terry
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

You just need a typecast like

Server server = (Server) serverlist.get(0);


[Jess in Action][AskingGoodQuestions]
Terry Chambers
Greenhorn

Joined: May 11, 2009
Posts: 17

Ok, well, that was way too easy.

Thank you for that.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

Ofcourse it would have been better when generics would have been used - Config.getServers() should have returned a List<Server> instead of a plain List. Then you wouldn't have needed the type cast.

Type casting should be avoided as much as possible; when you have a type cast in your code, there's something that's not type safe.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with List class
 
Similar Threads
question on server binding
JavaBean and JSP.
Background classes
passing array from processAction to jsp
please answer this 128 questions for WLS. Urgently!