Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javafx媒体播放导致NullpointerException(使用Swing)_Java_Swing_Javafx 8 - Fatal编程技术网

Javafx媒体播放导致NullpointerException(使用Swing)

Javafx媒体播放导致NullpointerException(使用Swing),java,swing,javafx-8,Java,Swing,Javafx 8,我打算使用JavaFX来播放mp3文件。由于某些原因,我不得不使用swing组件来构建UI;我现在使用JLayeredPane来重叠Jcomponents。所以我真的没有机会使用JFXPane 我检查并添加 但是java.lang.NullPointerException仍然存在 完整的痕迹 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException* *at ClockFrame$ClockMMPlayer.pla

我打算使用JavaFX来播放mp3文件。由于某些原因,我不得不使用swing组件来构建UI;我现在使用JLayeredPane来重叠Jcomponents。所以我真的没有机会使用JFXPane

我检查并添加

但是java.lang.NullPointerException仍然存在

完整的痕迹

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException*
*at ClockFrame$ClockMMPlayer.play(ClockFrame.java:110)
at ClockFrame.mouseDragged(ClockFrame.java:148)
at java.awt.Component.processMouseMotionEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)*** 
欢迎您的任何意见

alarm.mp3

如何使用测试代码:在面板上运行并拖动鼠标,然后显示NullPointerException

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.*;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

public class ClockFrame extends JFrame implements MouseListener,     MouseMotionListener{
    ClockMMPlayer mmPlayer;
    public ClockFrame(){
        int frameWidth = 1000;
        int frameHeight=1000;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setPreferredSize(new Dimension(frameWidth, frameHeight));
        pack();
        setVisible(true);           
        addMouseMotionListener(this);
        addMouseListener(this);        
        mmPlayer=new ClockMMPlayer("./res/sound/alarm.mp3");
}

 class ClockMMPlayer extends Application{
        MediaPlayer mediaPlayer;
        Media media;
        JFXPanel fxPanel;
        public void start(Stage stage) {

        }
        public ClockMMPlayer(String filePath) {
            assert filePath.length()>0; 
            JFXPanel fxPanel = new JFXPanel();
            Platform.setImplicitExit(false);
            Media m = new Media(new File(filePath).toURI().toString());
            MediaPlayer mediaPlayer = new MediaPlayer(m);
        }

        public void play(){mediaPlayer.play();}
 }
@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub


}


@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
}


@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

@Override
public void mouseDragged(MouseEvent e) {

    mmPlayer.play();

}


@Override
public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
public static void main(String[] a) {
    new ClockFrame();
}



}

请发布并包含完整的堆栈跟踪。是否确定
m
不为空?可能在您尝试访问的路径上找不到该文件。我已使用代码进行检查,file varTmpDir=new file(“/var/tmp”);boolean exists=varTmpDir.exists();它证明了java文件的存在。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.*;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

public class ClockFrame extends JFrame implements MouseListener,     MouseMotionListener{
    ClockMMPlayer mmPlayer;
    public ClockFrame(){
        int frameWidth = 1000;
        int frameHeight=1000;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setPreferredSize(new Dimension(frameWidth, frameHeight));
        pack();
        setVisible(true);           
        addMouseMotionListener(this);
        addMouseListener(this);        
        mmPlayer=new ClockMMPlayer("./res/sound/alarm.mp3");
}

 class ClockMMPlayer extends Application{
        MediaPlayer mediaPlayer;
        Media media;
        JFXPanel fxPanel;
        public void start(Stage stage) {

        }
        public ClockMMPlayer(String filePath) {
            assert filePath.length()>0; 
            JFXPanel fxPanel = new JFXPanel();
            Platform.setImplicitExit(false);
            Media m = new Media(new File(filePath).toURI().toString());
            MediaPlayer mediaPlayer = new MediaPlayer(m);
        }

        public void play(){mediaPlayer.play();}
 }
@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub


}


@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
}


@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

@Override
public void mouseDragged(MouseEvent e) {

    mmPlayer.play();

}


@Override
public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
public static void main(String[] a) {
    new ClockFrame();
}



}