EDIT: resolved
on a side note, how do i pause an audio stream? is there a way, or can i only kill the player?
this school project i am doing, it's a music player, it is in BlueJ. the requirements were to have a form displayed with a combo box, play buttons,etc.
i managed to get my playlist of audio into the playlist, but trying to change the SelectedIndex is giving me issues.
for example, there are two play buttons.
one is a play button- you play the first song in the playlist. for this, i need to use the method GetSelectedIndex() to get me the index to play the song. the items in the combo box are in the same order as the files are listed.
the other button is a play random button, once pressed, it will play a random song in the given library. this button works, but when it plays the song, it needs to display which song it is playing in the combobox. using the SetSelectedIndex() method.
both are giving me the java.lang.NullPointerException error. being int values, i can't seem to figure out how to make the "object" instanced- this is what other answers were saying. (on other questions)
random button constructor: (the other button is very similar to this)
private JButton ranbutton;
ranbutton = new JButton();
ranbutton.setMargin(new Insets(0,0,0,0));
ranbutton.setText("ran");
ranbutton.addActionListener(sl);
ranbutton.setBounds(150,220,35,30);
ShuffleListener sl= new ShuffleListener();
Action Listener of random button:
private class ShuffleListener implements ActionListener
{
public void actionPerformed(ActionEvent t)
{
org.playrandom();
playlist.setSelectedIndex(1); **THIS GIVES ME THE ERROR**
System.out.println("Z");
}
because it gives me a null error at the set selected index, "Z" never gets printed.
the other play button, is defined the same, and the action listener is the same except for the one difference:
int w= playlist.getSelectedIndex();**THIS GIVES ME THE ERROR**
code for the JComboBox "playlist" note: this code works. it is moldable to the number of files in the audio folder. note#2: org is the instance of the organizer class - which controls everything and it works.
public JComboBox playlist;
Integer[] nums;
x= org.getNumberOfTracks();
nums = new Integer[x];
String item[] = new String[x];
for(int i = 0 ; i < x; i++) {
item[i]= ((org.getName(i)));
}
JComboBox playlist = new JComboBox(item);
playlist.setBounds(50,50,250,50);
Now, something interesting, if i put
playlist.setSelectedIndex(4);
in the bottom of the constructor, and run the program, the combo box will be set to index 4 (item 5)
i have messed around a bit, isolating the code when it is called, and using System.Out.println()
to see if the code is going through.
but i cannot figure out why it will call in the constructor but not in the ActionListener class.
Here is all of the code, excuse the formatting:
import java.util.ArrayList;
import java.util.Random;
import java.util.Iterator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Insets;
import java.lang.Integer;
public class GUI extends JFrame
{
private int x;
private int r;
private int p;
private String l;
private int n=0;
private JButton playbutton;
private JButton ranbutton;
private JButton prevbutton;
private JButton skipbutton;
private JButton stopbutton;
private JButton repeatbutton;
private static MusicOrganizer org;
public JComboBox playlist;
public boolean looper=false;
public static void main(String[] args)
{
new GUI();
}
public GUI()
{
org= new MusicOrganizer();
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Awesome Music Player");
this.setLocation(600,300);
JPanel panel= new JPanel();
panel.setLayout(null);
ButtonListener blp = new ButtonListener();
ShuffleListener sl= new ShuffleListener();
StopListener pl= new StopListener();
RepListener rp = new RepListener();
playbutton= new JButton();
playbutton.setMargin(new Insets(0,0,0,0));
playbutton.setText("►");
playbutton.addActionListener(blp);
playbutton.setBounds(35,215,40,40);
repeatbutton= new JButton();
repeatbutton.setMargin(new Insets(0,0,0,0));
repeatbutton.setText("○");
repeatbutton.addActionListener(rp);
repeatbutton.setBounds(190,220,35,30);
skipbutton = new JButton();
skipbutton.setMargin(new Insets(0,0,0,0));
skipbutton.setText(">>|");
skipbutton.setBounds(75,220,30,30);
prevbutton = new JButton();
prevbutton.setMargin(new Insets(0,0,0,0));
prevbutton.setText("|<<");
prevbutton.setBounds(5,220,30,30);
ranbutton = new JButton();
ranbutton.setMargin(new Insets(0,0,0,0));
ranbutton.setText("ran");
ranbutton.addActionListener(sl);
ranbutton.setBounds(150,220,35,30);
stopbutton= new JButton();
stopbutton.setMargin(new Insets(0,0,0,0));
stopbutton.setText("■");
stopbutton.addActionListener(pl);
stopbutton.setBounds(110,220,35,30);
Integer[] nums;
x= org.getNumberOfTracks();
nums = new Integer[x];
String item[] = new String[x];
for(int i = 0 ; i < x; i++)
{
item[i]= ((org.getName(i)));
}
JComboBox playlist = new JComboBox(item);
playlist.setBounds(50,50,250,50);
panel.add(skipbutton);
panel.add(prevbutton);
panel.add(playbutton);
panel.add(ranbutton);
panel.add(playlist);
panel.add(stopbutton);
panel.add(repeatbutton);
this.add(panel);
this.setVisible(true);
//playlist.setSelectedIndex(4);(the code that will work....)
}
public void u()
{
playlist.setSelectedIndex(1);//( temp testing)
}
public void q()
{
n=(int)playlist.getSelectedIndex();//( temp testing)
System.out.println(n);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (x==1)
{
playbutton.setText("| |");
int w= playlist.getSelectedIndex(); //THIS GIVES ERROR
System.out.println(w);
x=0;
}
else
{
playbutton.setText("►");
x=1;
}
}
}
private class ShuffleListener implements ActionListener
{
public void actionPerformed(ActionEvent t)
{
org.playrandom();
u();//-> method is playlist.setSelectedIndex(1); gives me the error
System.out.println("I");
}
}
private class StopListener implements ActionListener
{
public void actionPerformed(ActionEvent c)
{
org.stopPlaying();
}
}
}