Results 1 to 3 of 3
  1. #1
    TSTONESIX is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default error calling constructors from method

    I'm having an error that pops up trying to use a generic data type in a constructor which I am calling from a method (line 54). The error happens when the method is trying to call on another method (lines 23-29) in the class to create an object. I can type in another method that uses "String" (line 30-36) as a data type rather than the generic type "E", but I know there is a way to do it without having two methods that do the exact same thing. I also get an additional error in my main method ("The method add(String) is ambiguous for the type LinkedList<String>") when I try to run it with both methods.

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class LinkedList<E> {
        
        ListNode<E> first;
        ListNode<E> last;
        int listSize;
        
        public LinkedList(){
       	 first = last = new ListNode<E>(null);
       	 listSize = 0;
        }
        
        public LinkedList(String file){
       	 first = last = new ListNode<E>(null);
       	 listSize = 0;
       	 getList(file);
        }
        
        public void add(E data){
       	 ListNode<E> temp = new ListNode(data);
       	 last.setNext(temp);
       	 last = temp;
       	 listSize++;
       	 return;
        }
        public void add(String data){
       	ListNode<E> temp = new ListNode(data);
       	last.setNext(temp);
       	last = temp;
       	listSize++;
       	return;
        }
        
        public void getList(String listName){
       	 
       	 try{
       		 
       		 //ArrayList<ListNode> temp = new ArrayList();
    
            	String fileName = "//Users//zerosdamnation//Documents//workspace//CS367//bin//Project2//" + listName + ".txt";
            	File srcFile = new File(fileName);
            	Scanner fileIn = new Scanner(srcFile);
             	// for reading from the file
           	 
          	 
          	 
            	while (fileIn.hasNextLine()){
                	String inLine = fileIn.nextLine();
                	//System.out.println(inLine);
                	add(inLine);          	 
            	}
         	 
       	 }
        	catch (FileNotFoundException e){
            	System.out.println("No Such File");
        	}   	 
        }    
        public int getSize(){
        	return listSize;
        }
    }

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

    Default Re: error calling constructors from method

    Consider showing the full error message text, and indicating exactly which line causes it.

    Edit: I believe that your problem is that you can't have a generic LinkedList read in and accept String data.

    Edit 2: What does the ListNode<E> class look like? In particular what does it's constructor look like?
    Last edited by Fubarable; 03-15-2013 at 12:53 AM.

  3. #3
    TSTONESIX is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: error calling constructors from method

    I managed to fix the error that I had posted above, but I am still having an issue with casting as a String and I think it is related to the same issue I had above. The current error message I get when I try to add an object to my queue is as follows:

    Exception in thread "main" java.lang.ClassCastException: Project2.ListNode cannot be cast to java.lang.String
    at Project2.MovieQueueMain.main(MovieQueueMain.java:7 4)

    Here is the code for the constructors in the ListNode<E> class:

    Java Code:
    	ListNode(E data) {
    		this(data, null);
    	}
    	
    
    	ListNode(E data, ListNode<E> next) {
    		this.data = data;
    		this.next = next;
    	}

Similar Threads

  1. Replies: 6
    Last Post: 09-30-2011, 02:51 AM
  2. Replies: 2
    Last Post: 04-20-2011, 08:14 PM
  3. ERROR: Calling array from a method
    By agente47 in forum New To Java
    Replies: 2
    Last Post: 03-25-2011, 03:18 PM
  4. Replies: 10
    Last Post: 09-18-2008, 09:41 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
  •