File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ClassCastException" Watch "ClassCastException" New topic
Author

ClassCastException

Thorsten Jaeger
Greenhorn

Joined: Jan 04, 2012
Posts: 3
Hey Everybody,

I'm currently working on a program for random play of my MediaPlayList (Simpsons, Futurama, Music etc.). Therefore i wrote a subroutine, which is finding and filtering
all media files in a given folder:



This routine is taking every file which isn't .jpg or .cmd at the moment. Works, everything fine. (Perhaps you have an idea of how to make thisroutine smaller and perhaps nonrecursive oO ?)
Now, i want to filter things explicitly by their filetype, and I don't want to do it in the manner done here (with if(path not contains bla)) but with a method of file like f.isMediaFile().

I therefore wrote a MediaFile Class:



and changed the type of my get the files routine to



but now, I get a classcast error (Exception in thread "main" java.lang.ClassCastException: [Ljava.io.File; cannot be cast to [LMediaFile;) on:



direc is of type MediaFile, but will return a File Array. Where is my error in this concept, how can I circumvent this? Everthing I want is the easier checking if a File is a Media-File or not.

Please help and thanks in advance for your help

Regards,
CaZe


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Override listFiles (all 3 of them) to convert the File[] into a MediaFile[]:
Since the other listFiles methods will require a lot of the same code you should add a private method for converting a File[] into a MediaFile[].

However, I don't like this idea. Your MediaFile class exists only to make it easier for you to filter out non-media files. listFiles() already has support for that, using FileFilter and FilenameFilter. For instance:
Now List (bad name by the way, it should start with a lowercase L) will only contain File objects which are either directories or files that contain .jpg in the name but not .cmd.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Thorsten Jaeger
Greenhorn

Joined: Jan 04, 2012
Posts: 3
Thanks a lot Rob. That was the solution I was looking for. It's way more elegant than what I tried!

Thanks,
CaZe
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

You're welcome
Thorsten Jaeger
Greenhorn

Joined: Jan 04, 2012
Posts: 3
A thing in my mind, and with the code posted above I hope its resonable to post it here, is: Is it neccesary to count all the files first, then create the File-Array, and after that, filling it with all the files? It seems to me rather complicated. Isn't there an easier way?

Regards,
Thorsten
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Thorsten Jaeger wrote:A thing in my mind, and with the code posted above I hope its resonable to post it here, is: Is it neccesary to count all the files first, then create the File-Array, and after that, filling it with all the files? It seems to me rather complicated. Isn't there an easier way?

The easier way would be to use a List (e.g. ArrayList) instead of an array. Then you don't need to know how big it is in advance - you can just add files to it as necessary.
 
Don't get me started about those stupid light bulbs.
 
subject: ClassCastException
 
Similar Threads
Want to Edit cells in a JTable
phone listener is not working properly
brickus game help with logic and how to make a class
overriding JDBCRealm
why can display the file contents???