aspose file tools*
The moose likes JSP and the fly likes Cannot cast from String to ArrayList(JSP) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Cannot cast from String to ArrayList(JSP)" Watch "Cannot cast from String to ArrayList(JSP)" New topic
Author

Cannot cast from String to ArrayList(JSP)

Omar Perez
Ranch Hand

Joined: Jul 08, 2011
Posts: 36
Good day,

I'm actually trying to complete the excersise of the Servlets and JSP book in page 303 but I'm getting the following error in Eclipse Cannot cast from String to ArrayList(JSP).

Here is the code


The error as it appears in line <% ArrayList al = (ArrayList)request.getParameter("Names"); %>

I don't know what's wrong, because is exactly as is written in the solution in the book.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

A request parameter is never a List (or ArrayList), but always a String*. If the book says that you should cast the request parameter to an ArrayList, then that book has an error in it. Are you sure you shouldn't read an attribute instead of a parameter?

* Or a String[] when you're using getParameterValues


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jack Numen
Ranch Hand

Joined: Nov 09, 2011
Posts: 42


Thats a valid error since request.getParameter returns a String Object (please go through the API-documentation of HttpServletRequest).
You cannot cast a string to an ArrayList.

If you want to fetch more than one value in request object try using request.getParameterNames (returns enumeration)/ request.getParameterValues()(returns String array).

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot cast from String to ArrayList(JSP)