4

I am novice at Java programming, and I'm trying to add a list of files to a crude little media player. Seemingly I've succeeded in achieving what I want with this:

public class MusicPlayerGUI
    implements ActionListener
{
    private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));

    private JList tracklist;
    private MusicPlayer player;
    private FileOrganizer organizer;
    private List<String> tracks;
    private JFrame frame;

public MusicPlayerGUI()
{
    player = new MusicPlayer();
    organizer = new FileOrganizer();
    tracks = organizer.listAllFiles();
    makeFrame();
}

//Some methods omitted

public void play()
{
    int fileToPlay = tracklist.getSelectedIndex();
    String filenameToPlay = organizer.getFile(fileToPlay);
    player.play(filenameToPlay);
}

public void setupList()
{
    tracks = organizer.listAllFiles();
    String[] fileList = listAllTracks(tracks);
    tracklist.setListData(fileList);
}

public String[] listAllTracks(List<String> tracks)
{
    int numTracks = tracks.size();
    String[] fileList = new String[numTracks];
    for(int i = 0; i < numTracks; i++) {
        String field = tracks.get(i).toString();

        fileList[i] = field;
    }

    return fileList;
}

But when I compile, even if it does compile, it gives me an error stating:

[pathname]/classname.java uses unchecked or unsafe operations. Recompile with Xlint:unchecked for details

And everything in my player works, except it won't play the files, so I'm thinking that there might be a connection between the compiler warning I get, and the fact that the files won't play. Can anyone spot what I'm getting wrong?

Rohan Singh
  • 20,497
  • 1
  • 41
  • 48
SorenRomer
  • 217
  • 1
  • 10
  • 2
    Where are you getting that warning? – Rohit Jain Jan 14 '14 at 16:38
  • It pops up in a separate window. I'm using BlueJ if that has something to say... – SorenRomer Jan 14 '14 at 16:40
  • The error is only about the usage of Generics. All this can result in is a `ClassCastException` at runtime. – Marko Topolnik Jan 14 '14 at 16:42
  • This compile warning will not solve your problems with not playing any track. Looking at the code, my guess is that organizer.getTracklist() is not typesafe () and causes the warning. – proko Jan 14 '14 at 16:44
  • As Marko says, don't worry about the warning. It's hard to say why your file isn't playing without seeing your player, but in the past when I've tried that sort of thing it has been because I haven't put some kind of 'thread.sleep' in the player thread, so it terminates almost as soon as it starts. Just a side note, unless you are using a very old version of Java, you can replace your for loop with the cleaner 'for(String field : tracks) { fileList[i] = field; }' – Matt Jan 14 '14 at 16:49

3 Answers3

3

This happens when you assign a raw version of something to a version with a generic type parameter, like this:

List<String> list = new ArrayList(); // Note missing <String>

So my guess is that the listAllFiles method of FileOrganizer (whatever that is) returns List, not List<String>. Edit: Or user1631616 may have spotted it with your use of JList.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

This is due to the fact that you define your JList without generics.

Sully Brooks
  • 425
  • 4
  • 8
  • 21
Алексей
  • 1,847
  • 12
  • 15
2

The parameterized type is missing in private JList tracklist; Try to use parameterized type in generic class. private JList<Object> tracklist;

Qadir Hussain
  • 1,263
  • 1
  • 10
  • 26