• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

CastException: java.lang.String can not be cast to class

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greeting,
I have again posted my query with proper format.I have attached the code and error message. when servlet calls the view "xy.jsp"; This
view is not working and throwing a CastException.
I have identified the error point "it is at <jsp:useBean>". I have simply set a variable "result" to session at servlet and calls a View(xy.jsp). I want to fetch the value of "result" at JSP but only with the help of <jsp:useBean>.Kindly help me to resolve it.



this is a class "abc" inside "myPack" Package. it contains business logic which is being called




 
Ranch Hand
Posts: 334
2
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all may I suggest you use code tags around most of your post, it makes it much easier to read and provides line numbers so we can point to a section easier.

A CastException like A cannot be cast to B usually means you're trying to use = to do something it can't. In your case somewhere in there an abc object is being assigned a string but it's so hard to read I haven't found the exact line.

Joe
 
Prateek abhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Joe, I have mentioned the line with red color where exception is occurring.Kindly find it with Line numbers
I have attached the code and error message. when servlet calls the view "xy.jsp"; This
view is not working and throwing a CastException.Kindly help me to resolve it.

Index.html
---------------
1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3. <html>
4. <head>
5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6. <title>JSP Page</title>
7. </head>
8. <body>
9. <form action="MyController">
10 .<input type="text" name="name"><br>
11. <input type="submit" name="b1" value="submit">
12. </form>
13. </body>
14. </html>

-----------
MyController.java (Servlet Program)
--------------------------------------
1. import java.io.IOException;
2. import java.io.PrintWriter;
3. import javax.servlet.ServletException;
4. import javax.servlet.http.HttpServlet;
5. import javax.servlet.http.HttpServletRequest;
6. import javax.servlet.http.HttpServletResponse;
7. import javax.servlet.http.HttpSession;
8. import myPack.abc;
9. public class MyController extends HttpServlet {
10. protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
11. response.setContentType("text/html;charset=UTF-8");
12. PrintWriter out = response.getWriter();
13. abc d=new abc();
14. d.setName((String)request.getParameter("name"));
15. String s=(String) d.getName();
16. HttpSession session=request.getSession();
17. session.setAttribute("result",s);
18. response.sendRedirect("xy.jsp");
19. }
20. @Override
21. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22. processRequest(request, response);
23. }
24. @Override
25. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
26. processRequest(request, response);
27. }
28. @Override
29. public String getServletInfo() {
30. return "Short description";
31. }
32. }

-----------------
Business logic class (myPack.abc)
--------------------------------------
1. public class abc {
2. public abc() {
3. }
4. public String name = "";
5. public String getName() {
6. return name;
7. }
8. public void setName(String name) {
9. this.name = name;
10. }
11. }

-----------------------
Presentation Logic (xy.jsp)
----------------------------------
1. <%@page contentType="text/html" pageEncoding="UTF-8" import="myPack.abc" %>
2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3. <html>
4. <head>
5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6. <title>JSP Page</title>
7. </head>
8. <body>
9. <jsp:useBean id="result" class="myPack.abc" scope="session"></jsp:useBean>
10. <jsp:getProperty property="name" name="result"></jsp:getProperty>
11. </body>
12. </html>
----------------------------------------
Error Page
-------------------
1. HTTP Status 500 -
2. type Exception report
3. message
4. descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
5. exception
6. org.apache.jasper.JasperException: java.lang.ClassCastException: java.lang.String cannot be cast to myPack.abc
7. root cause
8. java.lang.ClassCastException: java.lang.String cannot be cast to myPack.abc
9. note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.0.1 logs.
10. GlassFish Server Open Source Edition 3.0.1
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prateek. Welcome to the Ranch!

Have a look at UseCodeTags - that tells you the best way to make your code readable.

Anyway, I think your problem is a combination of this:

and this

You're adding a String as the "result" session attribute. And then in the JSP you try and say it's a myPack.abc. So the server tries to perform a cast, and that fails because it isn't a myPack.abc
 
Prateek abhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Mattew, You have identified the same line which i have also done. But i believe that the code what i have written is ok. at Jsp Container will look request first and find if any attribute is set or not.It will find
"result". If i declare id="result" then it will work like object of "myPack.abc". but why its not working....i want to fetch the value of result(sent by servlet) at jsp...kindly help.
As i am completely new here hence i dont know about tag codes.Thanks for the link i will follow it
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't magically convert a String to an instance of another class just by declaring it of that type. If you try to then you'll get a ClassCastException, which is exactly what you're seeing.

If you want to extract a session attribute as a particular type, you need to add it as that type in the first place.
 
Prateek abhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hello Mattew,

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I don't mean that. That would cause exactly the same problem - you'd be trying to cast a String to an abc.

If you want the session to contain an abc object, the code you need to change is where you add the attribute to the session. You need to add an abc at that point, instead of adding a String. It's your servlet code that needs to change.
 
Prateek abhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Matthew,
I have modified the code as...and its working fine....
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic