0

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


}
Bryce
  • 1
  • 1
  • 2
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Nathan Tuggy Feb 03 '15 at 00:32
  • 1
    Since you don't tell us precisely which statements are getting the exceptions, it's hard to say for sure, but I *think* it's this one `playlist.setSelectedIndex(1);` and it's twin. If so, `playlist` is null. Not hard at all to figure that out. – Hot Licks Feb 03 '15 at 00:37
  • Need to show more code. Need to see where the `playlist` variable is initialised. Also need to see the *full* methods where the `Null Pointer Exception` is occurring. And your snippet of the `playlist` class seems unusual. – jnd Feb 03 '15 at 00:37
  • How does the button get a copy of the playlist? Your button constructor doesn't have a playlist argument. So, playlist in your button is null after the constructor runs. Unless you have some button method that sets the playlist somewhere that gets called before you are able to click the button. . . – iheanyi Feb 03 '15 at 00:38
  • the button does not. the playlist gets a copy – Bryce Feb 03 '15 at 00:39
  • The playlist gets a copy of the button - for what purpose? In any case, it doesn't matter because you are modifying a playlist from within a Button, therefore, a Button must have a copy of the playlist reference to be able to modify it. – iheanyi Feb 03 '15 at 00:40
  • okay well how do i do that? the playlist, takes a copy of the library to display it to the user, the code i have there creates a array, and takes the names of the songs – Bryce Feb 03 '15 at 00:49
  • Your code looks setup/formatted fine to me. Listen to your "french speaking physics teacher." – Azar Feb 03 '15 at 01:07
  • The "full code" can have some indention love. We actually dont care how you have to format your code as long as you publish it readable here. I would suggest you use Ctrl+I in eclipse to fix it before you copy it. (I also would remove unlreated variables, but since this is already answered this might be optional :) – eckes Feb 03 '15 at 01:28

1 Answers1

2

The playlist you set in GUI constructor is a local variable within the constructor, not the field. The field is never initialized. In the statement JComboBox playlist = new JComboBox(item); remove the first JComboBox making it not a declaration of a new variable but a reference. The line should read playlist = new JComboBox(item);

Lev Kuznetsov
  • 3,520
  • 5
  • 20
  • 33
  • thank you! this project is going to be the end of me. in 4 cases has one word given me 2hour long troubles. – Bryce Feb 03 '15 at 02:24