0

This programs take in a string for a parameter. Currently filled with:

"http://localhost/media/svu.mp4"

I have made sure the URL exists.

I am using the VLCj library to create the mediaPlayerComponent (which is placed inside the container (JPanel mainPanel) ). The component mainPanel is then placed inside the JLayeredPanel layers. On top of that, I place a clear (non-opaque) layer (JPanel glassPane). According to everything I've read, this should be working and Eclipse isn't showing any errors or warnings.

The stack trace is as follows:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
   at client.test.Client.<init>(Client.java:62)
   at client.test.Client$1.run(Client.java:44)
   at java.awt.event.InvocationEvent.dispatch(Unknown Source)
   at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
   ....

The code is below. Line 62 is marked with a comment. The JPanels and JLayeredPanel, as well as the windowDimensions are all created as static objects above the main method in my code.

Any and all help is greatly appreciated.

static JLayeredPane layers = new JLayeredPane();
static JPanel mainPanel, glassPane = new JPanel();

public Client(String toPlay) {
    JFrame frame = new JFrame("Client");
    mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
    MediaPlayer mediaPlayer= mediaPlayerComponent.getMediaPlayer();

    frame.setSize(windowDimensions[0], windowDimensions[1]);
    frame.setLayout(new BorderLayout());
    frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
    frame.add(layers, BorderLayout.CENTER);

    layers.setBounds(0,0,windowDimensions[0], windowDimensions[1]);
    mainPanel.setBackground(Color.black); /* This is line 62 */
    mainPanel.setBounds(0,0,windowDimensions[0], windowDimensions[1]);
    mainPanel.setOpaque(true);
    mainPanel.add(mediaPlayerComponent);

    glassPane.setBackground(Color.white);
    glassPane.setBounds(0,0,windowDimensions[0], windowDimensions[1]);
    glassPane.setOpaque(false);

    layers.add(mainPanel, new Integer(0), 0);
    layers.add(glassPane, new Integer(1), 0);

    frame.setVisible(true);
    mediaPlayer.playMedia(toPlay);
}
Nick
  • 191
  • 1
  • 2
  • 9

2 Answers2

2

mainPanel has not been intitialized, even if it looks like it has. You have this code:

static JPanel mainPanel, glassPane = new JPanel();

This only inititalzes glassPane. In order to initialize mainPanel you have to change your code to this:

static JPanel mainPanel = new JPanel(), glassPane = new JPanel();
BitNinja
  • 1,477
  • 1
  • 19
  • 25
  • I'm gonna choose @David Xu for best answer because he called it first, but you're absolutely correct. Execution went without a hitch! Thanks a lot guys! – Nick May 30 '14 at 18:20
  • @NickMedovich As you should, he beat me to it :). – BitNinja May 30 '14 at 18:26
1

You haven't initialized mainPanel. Try adding mainPanel = new JPanel(); above the error line.

You'll also need to call frame.add(mainPanel); after you initialize the panel.

David Xu
  • 5,555
  • 3
  • 28
  • 50
  • I initialize each of the panels statically above the main method: JPanel mainPanel, glassPane = new JPanel(); – Nick May 30 '14 at 18:10
  • The only way that line is throwing an NPE is if mainPanel is null. – David Xu May 30 '14 at 18:11
  • Well at least the form is showing now. Adding the mainPanel initialization to its own line made the form show up. Now I just have to figure out why the video isn't loading. Thank you very much!! – Nick May 30 '14 at 18:14
  • And just to throw it out there, I don't add the mainPanel to the frame at all. I add the mainPanel to the JLayeredPanel, add the glassPane on top, then add the whole JLayeredPanel to the frame. – Nick May 30 '14 at 18:16