Forum Stats

  • 3,837,196 Users
  • 2,262,234 Discussions
  • 7,900,219 Comments

Discussions

Cannot cast from ActionForm to StudentForm

807603
807603 Member Posts: 44,391
edited Jan 15, 2005 9:11PM in Java Programming
Hi All,
I am getting this Eroor


Cannot cast from ActionForm to StudentForm

public ActionForward execute(ActionMapping mapping,
	                                 ActionForm form,
	                                 HttpServletRequest request,
	                                 HttpServletResponse response)
	        throws Exception {
	        StudentService service = new StudentService();
	        StudentForm studentForm = (StudentForm) form; //Error is shown here...
	        StudentDTO studentDTO = new StudentDTO();
	        BeanUtils.copyProperties( studentDTO, studentForm );
	        service.insertStudent( studentDTO );
	        request.setAttribute("employee",studentDTO);
	        return (mapping.findForward("success"));
	    }
Any help would be greatly appreciated.

Thanks
AS

Comments

  • 796447
    796447 Member Posts: 10,442
    Ok, so form must not be a StudentForm object. Just like you can't do this:
    Object o = new Object();
    String s = (String) o;

    What made you think that the object passed to that method really is a StudentForm object?
  • 807603
    807603 Member Posts: 44,391
    Ok, so form must not be a StudentForm object. Just
    like you can't do this:
    Object o = new Object();
    String s = (String) o;

    What made you think that the object passed to that
    method really is a StudentForm object?
    Thanks for the reply.I dont quite understand what u mean .........
    hopefully u explain in more detail

    AS
  • 796447
    796447 Member Posts: 10,442
    Are you under the impression that you can just cast from any object type to another? You can't. So unless the actual object type passed to that method really is a StudentForm object (or a subclass of StudentForm), you can't just willy-nilly cast it and expect it to work.
  • 807603
    807603 Member Posts: 44,391
    Are you under the impression that you can just cast
    from any object type to another? You can't. So unless
    the actual object type passed to that method really
    is a StudentForm object (or a subclass of
    StudentForm), you can't just willy-nilly cast it and
    expect it to work.
    Thanks for the reply again

    I think i am getting what u r saying here....

    StudentForm is a subclass of Action, so i guess it is the actual object type StudentForm, let me know whether i am right with this, and dont be harsh

    Thanks
    AS
  • 796447
    796447 Member Posts: 10,442
    StudentForm is a subclass of Action, so i guess it is
    the actual object type StudentForm, let me know
    whether i am right with this, and dont be harsh
    Just because StudentForm is a subclass of Action doesn't mean the actual object passed is a StudentForm.
    class Action { ... }
    class StudentForm extends Action { ... }
    Action x = new Action();
    StudentForm y = (StudentForm) x; // can't do this - you (or whatever on your behalf) didn't actually create a StudentForm object.

    If you want to see what was actually passed to that method, do this:
    System.out.println("form is a: " + form.getClass());
    before that line where you're trying to cast it. Then examine the output and you'll see what kind of object was actually passed in.
  • 796447
    796447 Member Posts: 10,442
    (and you must have meant ActionForm, not Action - so adjust my reply accordingly)
This discussion has been closed.