Results 1 to 6 of 6

Thread: Object casting

  1. #1
    spiderweb is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default Object casting

    I have a list consisting of 2 objects, each object contains an Object(Type JobRunStat), Long, Long and String parameters

    So, I'm doing this:

    Java Code:
    List returnJobs = selectQuery.getResultList();
    for(int i = 0; i < returnJobs.size(); i++){
    ReturnedObject o = (ReturnedObject) returnJobs.get(i);
    And I get:
    [java] 15:33:02,453 ERROR [STDERR] java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to service.impl.JobRunServiceImpl$ReturnedObject

    This is my ReturnedObject class:

    Java Code:
    private class ReturnedObject{
    		public JobRunStat jobRunStat = null;
    		public Long contractId = new Long(0);
    		public Long packageId = new Long(0);
    		public String jobName = "";
    		
    		public ReturnedObject(){
    			
    		}
    			
    		public ReturnedObject(JobRunStat jobRunStat, Long contractId, Long packageId, String jobName){
    			this.jobRunStat = jobRunStat;
    			this.contractId = contractId;
    			this.packageId = packageId;
    			this.jobName = jobName;
    		}
    with appropriate getters and setters for each field. Solutions?:)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Out of curiosity, if you run this before your exception occurs:

    Java Code:
    List returnJobs = selectQuery.getResultList();
    System.out.println (returnJobs.get(0).getClass().getName());
    what is displayed? In other words, what objects is your list holding? They may not be what you think they are.

  3. #3
    spiderweb is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default

    [java] 16:27:19,015 INFO [STDOUT] [Ljava.lang.Object;

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Ping the code related to selectQuery object
    Ramya:cool:

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    We need to see the getResultList() method. It appears that your list does not contain ReturnedObjects, so we need to see how the list is created.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Casting
    By zzpprk in forum Advanced Java
    Replies: 13
    Last Post: 08-13-2009, 08:59 PM
  2. What does casting mean?
    By sev51 in forum New To Java
    Replies: 3
    Last Post: 01-27-2009, 05:31 PM
  3. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 12:07 AM
  4. Object to Int casting
    By nn12 in forum New To Java
    Replies: 4
    Last Post: 12-06-2008, 11:58 PM
  5. Casting
    By leebee in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 01:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •