• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Generic error!

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I compile my code it produces:

jakarta-tomcat-5\webapps\begjsp-tourism\WEB-INF\classes\com\wrox\tourism\db\Even
tDAO.java:217: warning: [unchecked] unchecked call to add(E) as a member of the
raw type java.util.ArrayList
list.add(event);
^
I thought I defined the gerneric types. What do I need to do to list.add(new Attraction(attraction));?
Here is the piece of code:

public Collection findAll() {

PreparedStatement ps = null;
ResultSet rs = null;
ArrayList list = new ArrayList();

String sql = "SELECT * from attraction ";

try {

if (con.isClosed()) {
throw new IllegalStateException("error.unexpected");
}

ps = con.prepareStatement(sql);
rs = ps.executeQuery();

while(rs.next()) {

Attraction attraction = new Attraction();
attraction.setUserId(rs.getString(1));
attraction.setPassword(rs.getString(2));
attraction.setName(rs.getString(3));
attraction.setDescription(rs.getString(4));
attraction.setWebSite(rs.getString(5));
attraction.setAddress(rs.getString(6));

list.add(new Attraction(attraction));

}

return list;
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try changing list.add(new Attraction(attraction)) to list.add(attraction)
 
Robert Bonslater
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, I also needed to use <Type_Name> tags after the array:

ArrayList<Attraction> list = new ArrayList<Attraction>();

Thanks
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any "generics" in your code...
The error state that you are using a raw non parameterized ArrayList...
If you want a generified ArrayList, then you have to include the generic parameters ( terminology anyone ? )...

now the ArrayList is set to take only Attraction types...

here is a link to Sun's Java Tutorial Generics Lesson...
http://java.sun.com/docs/books/tutorial/extra/generics/index.html

check it out so that you can learn what and how to use generics...

have fun...
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic