Exception in "AWT-EventQueue-0" java.lang.NullPointerException

763 Views Asked by At

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

There are 2 best solutions below

4
On BEST ANSWER

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.

2
On

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