简体   繁体   中英

“Cannot Find Symbol” error in constructor, and passing ArrayList from class to class

ok im having trouble passing arraylists between classes and im geting a nullPointer exception when i take the constructor out of the object creating in the main. i cant get the arraylist around while also being succesfully modified, or filled with the files it checks for in the directory, keep in mind im new at stackOverflow and programming in general, go easy on me please.

this is the main class

import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;
import javazoom.jl.player.*;
import org.apache.commons.io.IOUtils;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class StreamAudio
{

public static TextArea textArea;
public static ArrayList<File> files;

public StreamAudio()
{

    ArrayList<File> files = new ArrayList<File>();      


    File folder = new File("C:\\Users\\hunter\\Desktop\\code\\StreamAudio\\Music");     
    File[] allFiles = folder.listFiles();


    if(folder.isDirectory())
    {

        for (File file : allFiles)
        {
            if (file.isFile())
            {
                files.add(file);

            }
        }
    }               

    int count = 0;
    for(File i : files)
    {
        count++;
        textArea.append(files.get(count - 1).getName()+"\n");

    }       
}

public static void main(String[] args)
{   

    MusicGUI gooey = new MusicGUI(ArrayList<File> files);

}   

}

and this is the GUI class, can i also have some tips on organizing everything, im so messy

import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;
import javazoom.jl.player.*;
import org.apache.commons.io.IOUtils;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MusicGUI{

public static TextArea textArea;
public static ArrayList<File> files;

public MusicGUI(ArrayList<File> t)
{
    files = t;
}

public MusicGUI()
{   


JFrame frame = new JFrame("FrostMusic");
JButton next = new JButton("Next");
JPanel panel = new JPanel();
TextArea textArea = new TextArea("", 15, 80, TextArea.SCROLLBARS_VERTICAL_ONLY);
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);

//frame properties
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(650,450);
frame.setBackground(Color.white);



/////////////////////////    MUSIC CODE     /////////////////////////////


    String path = files.get(1).getPath();
    File song = new File(path);

    String name = song.getName();
    name.replace(".mp3", "");

//////////////////////////////////////////////////////////////////////  

JLabel label = new JLabel("Now Playing "+name);


//panel properties
panel.setBackground(Color.white);

//play button
JButton play = new JButton("Play");

try
{
    FileInputStream fis = new FileInputStream(song);
    BufferedInputStream bis = new BufferedInputStream(fis);

    String filename = song.getName();
    Player player = new Player(bis);



    play.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent jh)
        {

            try
            {

                player.play();

            }catch(Exception e){}   
        }
    });


    next.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent jk)
        {
            try
            {
                player.close();

            }catch(Exception e){}   

        }
    });

}catch(Exception e){}



panel.add(play);
panel.add(textArea);
panel.add(label);
panel.add(next);

frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);


}

}

Look at this line in the StreamAudio class inside the main method

MusicGUI gooey = new MusicGUI(ArrayList<File> files);

You cannot "declare" a variable inside the call to a constructor. Change it the following and it should work:

MusicGUI gooey = new MusicGUI(files);

You can only pass a reference to an object or a variable or literal as a parameter within a method or a constructor.


Update

I'm updating some code here. Try to see if this works for you.

Here's the StreamAudio class:

public class StreamAudio {

    private List<File> files;

    public StreamAudio() {
        files = new ArrayList<File>();

        File folder = new File("/Users/ananth/Music/Songs");
        File[] allFiles = folder.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".mp3");
            }
        });

        if (folder.isDirectory()) {
            for (File file : allFiles) {
                if (file.isFile()) {
                    files.add(file);
                }
            }
        }
        System.out.println(files);
    }

    public List<File> getFiles() {
        return files;
    }

    public static void main(String[] args) {
        StreamAudio streamAudio = new StreamAudio();
        MusicGUI gooey = new MusicGUI(streamAudio.getFiles());
        gooey.showUI();
    }

}

And here's the MusicGUI class:

public class MusicGUI {

    private TextArea textArea;
    private List<File> files;
    private JPanel panel;
    private Player player;

    private int index = -1;

    public MusicGUI(List<File> t) {
        files = t;
        init();
    }

    public void init() {
        panel = new JPanel();
        JButton next = new JButton("Next");
        textArea = new TextArea("", 15, 80, TextArea.SCROLLBARS_VERTICAL_ONLY);
        JScrollPane scrollPane = new JScrollPane(textArea);
        textArea.setEditable(false);

        JLabel label = new JLabel("Now Playing: ");

        // panel properties
        panel.setBackground(Color.white);

        // play button
        JButton play = new JButton("Play");

        play.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent jh) {
                String path = files.get(++index).getPath();
                ///////////////////////// MUSIC CODE ///////////////////////////////

                System.out.println("path: " + path);
                File song = new File(path);

                String name = song.getName();
                name.replace(".mp3", "");

                label.setText("Now Playing " + name);
                try {
                    FileInputStream fis = new FileInputStream(song);
                    BufferedInputStream bis = new BufferedInputStream(fis);

                    Player player = new Player(bis);
                    try {
                        player.play();
                    } catch (Exception e) {
                    }
                } catch (Exception e) {
                    System.out.println(e);
                }
                //////////////////////////////////////////////////////////////////////
            }
        });

        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent jk) {
                try {
                    player.close();
                } catch (Exception e) {
                }
                if(index==files.size()){
                    index = -1;
                }
                String path = files.get(++index).getPath();
                System.out.println("path: " + path);
                File song = new File(path);

                String name = song.getName();
                name.replace(".mp3", "");
                label.setText("Now Playing " + name);
            }
        });

        panel.add(play);
        panel.add(scrollPane);
        panel.add(label);
        panel.add(next);
    }

    public void showUI() {
        JFrame frame = new JFrame("FrostMusic");
        // frame properties
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(650, 450);
        frame.setBackground(Color.white);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM