构造函数中的“无法查找符号”错误,并将ArrayList从类传递给类

时间:2014-12-24 04:49:37

标签: java swing arraylist

好吧我无法在类之间传递arraylists并且当我从主要创建的对象中取出构造函数时,会出现一个nullPointer异常。我无法获得arraylist,同时也被成功修改,或填充它在目录中检查的文件,请记住我在stackOverflow的新功能和编程一般,请在我身上轻松。

这是主要的课程

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);

}   

}

这是GUI类,我也可以提供一些关于组织所有内容的技巧,非常混乱

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);


}

}

1 个答案:

答案 0 :(得分:1)

StreamAudio方法

中的main类中查看此行
MusicGUI gooey = new MusicGUI(ArrayList<File> files);

你不能宣布&#34;调用构造函数内部的变量。改变它以下它应该工作:

MusicGUI gooey = new MusicGUI(files);

您只能将对象或变量或文字的引用作为方法或构造函数中的参数传递。


更新

我在这里更新了一些代码。试着看看这是否适合你。

这是StreamAudio班级:

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();
    }

}

这里是MusicGUI班级:

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);
    }
}