How do I remove nullpointerexception

风流意气都作罢 提交于 2019-12-08 07:53:17

问题


I am trying to play a video using jmf. After working hard for hours literally removing all errors and exceptions, Here I am getting a null pointer exception. Here I figured out after looking on NullPointerException, this should be due to incorrect declaration of Player mediaPlayer where it is not initialised to anything.

Another problem with directly initialising it to what values I am giving it later is, I have to catch the exceptions too, then it says player might not be declared.

How do I declare mediaURL and Player so that this nullpointerexception is removed and I can play this video.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.media.*;
import java.net.URL;
import java.io.*;
import java.net.MalformedURLException;


    public class mediaPlayer extends JFrame
    {

    URL mediaURL;
    Player mediaPlayer;
        public mediaPlayer()
        {   
            JFrame f = new JFrame("new");
            f.setLayout(new BorderLayout());
            f.setSize(500,300);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //file you want to play
            try{
            String name = "file:///"+new File("output.mp4").getAbsolutePath();
                mediaURL = new URL(name);
            }catch (MalformedURLException ex){}
            //create the media player with the media url
            try{
                mediaPlayer = Manager.createRealizedPlayer(mediaURL);
            }catch(IOException ex){} catch(NoPlayerException ex){} catch(CannotRealizeException ex){}
            //get components for video and playback controls
            Component video = mediaPlayer.getVisualComponent();
            Component controls = mediaPlayer.getControlPanelComponent();
            add(video,BorderLayout.CENTER);
            add(controls,BorderLayout.SOUTH);
            f.setVisible(true);

        }
        public static void main(String[] args){
            new mediaPlayer();
        }
    }

Update2

javax.media.NotRealizedError: Cannot get visual component on an unrealized playe
r
        at com.sun.media.BasicPlayer.getVisualComponent(BasicPlayer.java:491)
        at com.sun.media.MediaPlayer.getVisualComponent(MediaPlayer.java:48)
        at mediaPlayer.<init>(mediaPlayer.java:29)
        at mediaPlayer.main(mediaPlayer.java:38)
Exception in thread "main" javax.media.NotRealizedError: Cannot get visual compo
nent on an unrealized player
        at com.sun.media.BasicPlayer.getVisualComponent(BasicPlayer.java:491)
        at com.sun.media.MediaPlayer.getVisualComponent(MediaPlayer.java:48)
        at mediaPlayer.<init>(mediaPlayer.java:29)
        at mediaPlayer.main(mediaPlayer.java:38)

Please help me in playing this video, removing this nullpointer exception. Thanks a lot to everyone who puts any effort on my problem:).


回答1:


Change your code to this,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.media.*;
import java.net.URL;
import java.io.*;
import java.net.MalformedURLException;


    public class mediaPlayer extends JFrame
    {
        public mediaPlayer()
        {   
        JFrame f = new JFrame("Video Demo");
            f.setLayout(new BorderLayout());
            f.setSize(500,300);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //file you want to play
            try{
                String name = "file:///"+new File("output.mp4").getAbsolutePath();
                URL mediaURL = new URL(name);
                Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);
            //get components for video and playback controls
            Component video = mediaPlayer.getVisualComponent();
            Component controls = mediaPlayer.getControlPanelComponent();
            f.add(video,BorderLayout.CENTER);
            f.add(controls,BorderLayout.SOUTH);
            } catch (Exception e) { e.printStackTrace(); }
            f.setVisible(true);

        }
        public static void main(String[] args){
            new mediaPlayer();
        }
    }

Credits-Andrew Thompson.




回答2:


Player is an interface so you cannot make its object. Instead use Manager.createPlayer(source) method to get your working. Here is the link , link




回答3:


If you check the JMF 2.1.1 - Supported Formats page, you'll notice a distinct lack of mention for MP4. Theoretically, fixing the problem as as 'simple' as obtaining a Service Provider Interface for MP4 files, and adding it to the run-time class-path.

The bottom line is that if this project is for playing a limited group of formats that are either inbuilt, or for which we can provide an SPI, it should be fine. OTOH JMF is not suitable as a 'general purpose player'. It is too old & supports too few formats.



来源:https://stackoverflow.com/questions/17897647/how-do-i-remove-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!