java - "Cannot Find Symbol"构造函数中出现错误,并将 ArrayList 在类之间传递

标签 java swing arraylist

好吧,我在类之间传递数组列表时遇到了麻烦,并且当我从 main 中创建的对象中取出构造函数时,我收到了 nullPointer 异常。我无法在成功修改数组列表的同时,也无法填充它在目录中检查的文件,请记住,我是 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);


}

}

最佳答案

查看 main 方法内的 StreamAudio 类中的这一行

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

您不能在构造函数的调用中“声明”变量。将其更改为以下内容,它应该可以工作:

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

关于java - "Cannot Find Symbol"构造函数中出现错误,并将 ArrayList 在类之间传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27631648/

相关文章:

java - JTextArea 不显示我告诉它的内容

java - ArrayList 有没有比 O(n) 更好的搜索方法?

Firebase Android 中的 java.lang.AbstractMethodError (FirebaseInstallationServiceClient.readGenerateAuthTokenResponse)

Java:使用非默认构造函数实例化通用对象

java - 如何自动将所有 Lucene TermQuery 对象转换为 PrefixQuery?

java - JLabel java 中以黑色显示的 GIF/PNG 图像的透明部分

java - Java中QuadCurve2D生成的曲线方程?

java - 单击某些 ListView 项目时如何调用特定号码

java - 可扩展数组在数组满时重新分配内存有什么作用?

java - 如何将 ArrayList 写入 XML 文件?