Как только нажата кнопка, я получаю NullPointerException
, где я ожидал, что значение в ArrayList
будет напечатано на экране. Например, если я нажму кнопку, указывающую на «A», на экране появится «A», если я нажму кнопку «B», на экране будет напечатан «B».Я хочу, чтобы значения в ArrayList показывались после нажатия кнопки
public class Media extends JPanel {
//Declares our media player component
private JPanel video_pnl, control_pnl;
private JButton play_btn;
private JLabel loc_lbl;
private int increment;
ArrayList<String> file_location;
public Media(ArrayList<String> file_location) {
this.file_location = file_location;
increment = 0;
while (increment < file_location.size()) {
video_pnl = new JPanel();
video_pnl.setLayout(new BoxLayout(video_pnl, BoxLayout.Y_AXIS));
loc_lbl = new JLabel();
loc_lbl.setText(file_location.get(increment));
play_btn = new JButton("Play");
control_pnl = new JPanel();
control_pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
play_btn.setActionCommand("play");
video_pnl.add(loc_lbl);
control_pnl.add(play_btn);
video_pnl.add(control_pnl, BorderLayout.SOUTH);
Handler handler = new Handler();
play_btn.addActionListener(handler);
video_pnl.revalidate();
add(video_pnl);
increment++;
}
}
private class Handler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("play")) {
play();
}
// if (e.getSource() == play_btn){
// play();
// }
}
}
public void play() {
for (int i = 0; i < file_location.size(); i++) {
System.out.println(file_location.get(i));
}
}
public static void main(String[] args) {
//Declare and initialize local variables
ArrayList<String> file_location = new ArrayList<>();
file_location.add("A");
file_location.add("B");
file_location.add("C");
file_location.add("D");
file_location.add("E");
//creates instances of the VlcPlayer object, pass the mediaPath and invokes the method "run"
Media mediaplayer = new Media(file_location);
JFrame ourframe = new JFrame();
ourframe.setContentPane(mediaplayer);
ourframe.setLayout(new GridLayout(5, 1));
ourframe.setSize(300, 560);
ourframe.setVisible(true);
ourframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Ну, где * исключение выбрасывается? Отправьте полную статистику стека и укажите соответствующие строки в вашем коде. (Вероятно, мы можем догадаться, но, пожалуйста, дайте полную информацию, чтобы упростить жизнь каждого человека - читайте http://tinyurl.com/so-list) –