当前位置: 动力学知识库 > 问答 > 编程问答 >

java - ArrayStoreException for no reason?

问题描述:

I am trying to make classes to do the following:

Get a folder, get what folder it contains, and for each folder that is in the main folder - get the files in it and move them into two groups - ones that are mp4, mkv, vlc and avi types and ones that are srt types.

My current code is:

Show.class: http://pastebin.com/vZ7ipFJc

Season.class: http://pastebin.com/tqUJ7S9v

Main.class: http://pastebin.com/LaEvuARW

The stacktrace leads me to many different places:

Exception in thread "main" java.lang.ArrayStoreException

at java.lang.System.arraycopy(Native Method)

at java.util.ArrayList.toArray(Unknown Source)

at Show.<init>(Show.java:47)

at Main.getShows(Main.java:24)

at Main.setupShowsBox(Main.java:70)

at Main.<init>(Main.java:96)

at Main.main(Main.java:54)

(The lines in pastebin are the same as in code) And the problem seems to be where I convert the arraylist into an array, and I dont know what the problem is. What is the problem and how can I fix it?

网友答案:

You have this line 47 in Show.java:

this.seasonsFile=seasons.toArray(new File[seasons.size()])

seasons is an ArrayList of Season. ArrayList#toArray is not going to be able to squeeze a load of Seasons into an array of Files. Maybe you wanted this.seasonsFile = seasons.getShows;?

Also, you shouldn't have package private variable names following the convention of method names. It's going to confuse everyone.

网友答案:

So here's the line causing the error:

List<Season> seasons;
File[] seasonFile;

this.seasonsFile=seasons.toArray(new File[seasons.size()]);

You're trying to convert a list of Seasons to an array of Files. You can convert a list of Files to an array of Files, or you can convert a list of Seasons to an array of Seasons using toArray().

If you have a way of converting Season objects to File objects you could put that in a toFile() method on your Season object. Then to get your file array:

int idx = 0;
for (season : seasons) {
    seasonsFile[idx] = season.toFile();
    idx++;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: