Results 1 to 6 of 6
  1. #1
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default ClassCastException

    This a bit complicated, but I keep getting a ClassCastException at the return of my getItemAt Method (in bold and underlined). CourseList implements DiaryList just as Course implements DiaryItem. I need to return a Diaryitem so I am upcasting a declared Course object from my Courselist which is a node from my linked list. I want to know a) Why can't I upcast Course to a DiaryItem? and b) How the h - e - double hockey sticks do I get around this? I can provide the other class codes if needed, just didn't want the post to be overwhelmed with code.

    Java Code:
    import java.util.*;
    import java.io.*;
    
    
    public class CourseList implements DiaryItemList {
    	private static LinkedNode head;
    	
    	private class LinkedNode {
    		private String name;
    		private String daystimes;
    		public Course data;
    		public LinkedNode link;
    		
    		public LinkedNode(Course o, LinkedNode link) {
    			data = o;
    			this.link = link;
    			if(o != null){
    			this.name = o.getName();
    			}
    		}
    	}
    	
    public CourseList() {
    		head = null;
    	}
    	public CourseList(String x){
    		 try {
    			   Course temp = new Course();
    			   FileReader from = new FileReader(x);
    			   BufferedReader input = new BufferedReader(from);
    			   String courseinfo = input.readLine();
    			   StringTokenizer cl = new StringTokenizer(courseinfo);
    			   
    			   String coname = cl.nextToken();
    			   temp.setName(coname);
    			   
    			   temp.setSection(cl.nextToken());
    			   String startT = cl.nextToken();
    			   startT = startT.replaceAll(":","");		
    			   startT.trim();
    			   int starter = temp.toMilitaryTime(startT);
    			   if(starter >= 1200){
    			    		startT = "" + startT + "PM";
    			   }
    			   else {
    			    		startT = "" + startT + "AM";
    			   }
    			   temp.setStartTime(starter);
    			   String endT = cl.nextToken();
    			   endT = endT.replaceAll(":","");
    			   endT.trim();
    			   int ender =temp.toMilitaryTime(endT);
    			   	if(ender >= 1200){
    			    	 endT = "" + endT + "PM";
    			   	}
    			   	else {
    			    		endT = "" + endT + "AM";
    			   	}
    			   temp.setEndTime(ender);
    			   String dayconvertor = cl.nextToken();
    			   dayconvertor = dayconvertor.replace('H','Y');
    			   dayconvertor = dayconvertor.replace('F','Z');
    			   temp.setDays(dayconvertor);
    			   courseinfo = input.readLine();
    			   insertAtFront(temp);
    			   System.out.print(temp);  
    		   }
    		   catch (FileNotFoundException e) {
    				System.out.println("File classy.txt not found");
    		   }
    		   catch(IOException e) {
    				System.err.println("Exception in reading from file");
    		   }	
    	}
    	public void insertAtFront(Course a){
    		LinkedNode newHead = new LinkedNode(a,head);
    		head = newHead;
    	}
    	public boolean isEmpty() {
    		if(head == null){
    		return false;
    		}
    		return true;
    	}
    
    public DiaryItem getItemAt(int index) {
    		LinkedNode p = head;
    		
    		int counter = 0;
    		while (p != null && counter != index){
    			p = p.link;
    			}
    		if(counter == index){
    			[B][U]return (DiaryItem) p;[/U][/B]
    		}
    		
    		return null;
    	}
    Last edited by MetalR0; 08-03-2011 at 02:38 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    It is beacause variable p refers to a LinkedNode not a DiaryItem. You cannot just cast an object from one type to another if there is no direct relationship between the two classes.

  3. #3
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    So then I just need to refer to the Course WITHIN the node that was created by the constructor! I think I can figure it out now. Thanks Junky!

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    One way around it would be to store a DairyItem in the LinkedNode and extract it from the LinkedNode to return it. But for some reason, you're storing a Course object in LinkedNode. There's possibly a design problem there...

  5. #5
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    I tried to return p.data instead since its a Course but still same type of error. I'll start looking reviewing the design...

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Where is the DiaryItem object stored? If it is stored in the Course object then you will need a chain of events. Get LinkedNode from the List -> get Course from LinkedNode -> get DiaryItem from Course

Similar Threads

  1. ClassCastException
    By Umi in forum New To Java
    Replies: 2
    Last Post: 01-14-2010, 12:51 AM
  2. ClassCastException: in EJB3
    By nida in forum Advanced Java
    Replies: 2
    Last Post: 07-03-2009, 04:03 PM
  3. ClassCastException
    By paulsim in forum Java Applets
    Replies: 2
    Last Post: 08-21-2008, 03:14 PM
  4. ClassCastException
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 06:26 AM
  5. ClassCastException
    By Felissa in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 06:06 AM

Posting Permissions

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