2 Replies - 1067 Views - Last Post: 30 November 2007 - 04:38 PM Rate Topic: -----

#1 thure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-November 07

Is the compiler looking in the wrong class?

Post icon  Posted 29 November 2007 - 05:10 PM

Hello

I am trying to compile what I have written, and I've encountered only two errors:

File: /Users/Will/Projects/Computers/a5/a5-basic-lib/a5-code/Playlist.java [line: 136]
Error: cannot find symbol
symbol : method createSong(java.lang.String)
location: class Playlist

and

File: /Users/Will/Projects/Computers/a5/a5-basic-lib/a5-code/Playlist.java [line: 152]
Error: cannot find symbol
symbol : method getSong(java.io.File)
location: class Playlist

Both of them refer to methods that I am calling from within Playlist.java, but the methods belong to other classes. I've called methods from the other classes successfully, but for some reason these two give me trouble.

The entire class is very long, so I'll just include the methods that I'm having trouble with and the import statements/constructors.

import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileReader;
import java.io.PrintWriter;
import comp202.fall2007.a5.comparator.*;
import comp202.fall2007.a5.filter.*;
import comp202.fall2007.a5.util.*;
import java.io.File;

public class Playlist {
  
  ArrayList<Song> playlist = new ArrayList<Song>();
  
  public Playlist () {
	
  }
  
  public Playlist (Playlist playlist) {
	
	playlist = playlist;
	
  }

  public java.util.ArrayList<String> load (java.io.File path, MusicCollection collection) {
	
	//first the playlist is cleared
	playlist.clear();
	
	//then the file is read
	try {
	  
	  FileReader reader = new FileReader (path);
	  Scanner scan = new Scanner(reader);
	  
	  //the playlist index advances as there are next lines in the file
	  for (int i=0; scan.hasNextLine(); i++) {
		
		//getting next line
		String nextPath = scan.nextLine();
		File nextFile = new File (nextPath);
		
		//does nothing if the line is empty
		if (nextPath.length() != 0) {
		  
		  //if a null is returned
		  if (null == collection.getSong(nextFile)) {
			
			try{
			  //adding the song's info to the playlist
			  playlist.set(i, createSong(nextPath));
			}
			
			//*SONGFACTORY ERRORS*//
			catch (IncorrectFileExtensionException ee){
			  System.out.println("The file " + nextPath + " had an incorrect file extension");
			}
			catch (java.io.IOException io){
			  System.out.println("There was an error reading the file " + nextPath);
			}
			//*END SONGFACTORY ERRORS*//
			
		  }
		  
		  else {
			//getting the song from the MusicCollection when it doesn't return null
			playlist.set(i, (getSong(nextFile)));
		  }
		  
		}//the line has either added to the playlist or has been skipped for being empty
		
	  }//all of the lines have been read in the file
	  
	}//the task has been completed
	
	//*SCANNER AND FILEREADER ERRORS*//
	catch (java.io.FileNotFoundException fnf){
	  System.out.println("The file " + path + " was not found");
	}
	catch (java.lang.IllegalStateException is){
	  System.out.println("An error has been encountered: Scanner Illegal State."); 
	}
	//*END SCANNER AND FILEREADER ERRORS*//
	
  }//end



I've looked everywhere and I can't seem to find the problem. I've verified that createSong is in comp202.fall2007.a5.util.SongFactory, which I've imported, and it does take a String. I've also verified that getSong is located in MusicCollection.java and it does take a java.io.File.

Thanks sincerely for any help!

This post has been edited by thure: 29 November 2007 - 05:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Is the compiler looking in the wrong class?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: Is the compiler looking in the wrong class?

Posted 29 November 2007 - 05:58 PM

The problem is that your createSong and getSong are being used in your program without being tied to an instance/static class.

try{
	 //adding the song's info to the playlist
	 playlist.set(i, createSong(nextPath));  <--- * Here you call createSong without being tied to something like myfactoryobject.createSong(nextPath), so it looks for createSong as a local function in Playlist... which it isn't.
}
			
//*SONGFACTORY ERRORS*//
catch (IncorrectFileExtensionException ee){
	 System.out.println("The file " + nextPath + " had an incorrect file extension");
}
catch (java.io.IOException io){
	 System.out.println("There was an error reading the file " + nextPath);
}
//*END SONGFACTORY ERRORS*//
			
}
		  
else {
	 //getting the song from the MusicCollection when it doesn't return null
	 playlist.set(i, (getSong(nextFile))); <--- * Here again you use getSong without being tied to an object. So it looks for getSong locally as a function of class Playlist... which it isn't.
}



I have highlighted the two errors here. Like I said before, you use these two functions without being tied to an object. So what it is assuming is that getSong and createSong are methods of the local class, Playlist. You didn't define them as part of Playlist. You have to create an instance of your factory class, or if declared as static methods, prefixed with the class name.

Hope this makes sense. :)
Was This Post Helpful? 0
  • +
  • -

#3 thure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-November 07

Re: Is the compiler looking in the wrong class?

Posted 30 November 2007 - 04:38 PM

You were absolutely right! I didn't realize SongFactory would have a constructor, and getSong was a silly mistake because I had used it correctly earlier in the program.

Thanks so much - I don't have any complier errors now! (enter ominous threat of runtime errors)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1